Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

[gmod-industrialization] addons/gmod-industrialization/lua/modules/uielements.lua:4:…

  1. User

    [gmod-industrialization] addons/gmod-industrialization/lua/modules/ui_elements.lua:4: attempt to call field 'AddNetworkString' (a nil value)

    1. unknown - addons/gmod-industrialization/lua/modules/ui_elements.lua:4
    2. include - [C]:-1 3. unknown - addons/gmod-industrialization/lua/autorun/industrialization_init.lua:47
    -- lua/modules/ui_elements.lua
    
    if CLIENT then
        util.AddNetworkString("Industrialization_UpdateInventory")
        util.AddNetworkString("Industrialization_UpdateFactory")
        util.AddNetworkString("Industrialization_UpdateSmelter")
        util.AddNetworkString("Industrialization_UpdateAssemblyMachine")
        util.AddNetworkString("Industrialization_UpdateHarvester")
        util.AddNetworkString("Industrialization_UpdatePowerStatus")
    
        -- Table to store player inventory
        local PlayerInventoryUI = {}
    
        -- Receive inventory updates from the server
        net.Receive("Industrialization_UpdateInventory", function()
            local inventory = net.ReadTable()
            PlayerInventoryUI = inventory -- Utilize the 'inventory' variable
            -- Refresh the inventory UI if it's open
            if IsValid(vgui.GetControl("DFrame", "Industrialization_InventoryFrame")) then
                local frame = vgui.GetControl("DFrame", "Industrialization_InventoryFrame")
                local list = frame:GetChildren()[1] -- Assuming the DListView is the first child
                list:Clear()
                for resource, qty in pairs(PlayerInventoryUI) do
                    if qty > 0 then
                        list:AddLine(ResourceTypes[resource].name or resource, qty)
                    end
                end
            end
        end)
    
        -- Function to create and display the inventory UI
        function CreateInventoryUI()
            if IsValid(vgui.GetControl("DFrame", "Industrialization_InventoryFrame")) then return end
    
            local frame = vgui.Create("DFrame")
            frame:SetTitle("Inventory")
            frame:SetSize(400, 600)
            frame:Center()
            frame:MakePopup()
            frame:SetName("Industrialization_InventoryFrame")
    
            local list = vgui.Create("DListView", frame)
            list:Dock(FILL)
            list:AddColumn("Resource")
            list:AddColumn("Quantity")
    
            for resource, qty in pairs(PlayerInventoryUI) do
                if qty > 0 then
                    list:AddLine(ResourceTypes[resource].name or resource, qty)
                end
            end
        end
    
        -- Bind a key to open the inventory (e.g., F1)
        hook.Add("PlayerButtonDown", "Industrialization_OpenInventory", function(ply, button)
            if ply ~= LocalPlayer() then return end
            if button == KEY_F1 then
                CreateInventoryUI()
            end
        end)
    
        -- Factory Control Panel (example)
        function CreateFactoryControlPanel(factory)
            if not IsValid(factory) then return end
            if IsValid(vgui.GetControl("DFrame", "Industrialization_ControlPanelFrame")) then return end
    
            local frame = vgui.Create("DFrame")
            frame:SetTitle("Factory Control Panel")
            frame:SetSize(300, 200)
            frame:Center()
            frame:MakePopup()
            frame:SetName("Industrialization_ControlPanelFrame")
    
            local startButton = vgui.Create("DButton", frame)
            startButton:SetText("Start Processing")
            startButton:SetPos(50, 50)
            startButton:SetSize(200, 30)
            startButton.DoClick = function()
                factory:StartProcessing()
                frame:Close()
            end
    
            local stopButton = vgui.Create("DButton", frame)
            stopButton:SetText("Stop Processing")
            stopButton:SetPos(50, 100)
            stopButton:SetSize(200, 30)
            stopButton.DoClick = function()
                factory:StopProcessing()
                frame:Close()
            end
        end
    
        -- Similar functions can be created for Smelter, Assembly Machine, etc.
    end
    
  2. Assistant

    The error you're encountering stems from attempting to call util.AddNetworkString on the client-side, where this function is undefined. In Garry's Mod, util.AddNetworkString is a server-side function used to register network messages that clients and servers can communicate with. Calling it on the client side results in the nil value error you're seeing.

    Here's a detailed explanation and steps to resolve the issue:

    Understanding the Error

    Error Message:

    addons/gmod-industrialization/lua/modules/ui_elements.lua:4: attempt to call field 'AddNetworkString' (a nil value)
    

    Root Cause:

    • Location of util.AddNetworkString Call: The util.AddNetworkString function is being called within an if CLIENT then block, which means it's executing on the client side.
    • Client vs. Server Scope: util.AddNetworkString is only available on the server. On the client side, this function doesn't exist, hence the nil value error.

    Solution

    1. Separate Server and Client Code

    You need to ensure that server-side functions like util.AddNetworkString are only executed on the server, and client-side functions are only executed on the client.

    2. Modify ui_elements.lua

    Here's how you can restructure your ui_elements.lua to separate server and client code properly:

    -- lua/modules/ui_elements.lua
    
    if SERVER then
        -- Register network strings on the server
        util.AddNetworkString("Industrialization_UpdateInventory")
        util.AddNetworkString("Industrialization_UpdateFactory")
        util.AddNetworkString("Industrialization_UpdateSmelter")
        util.AddNetworkString("Industrialization_UpdateAssemblyMachine")
        util.AddNetworkString("Industrialization_UpdateHarvester")
        util.AddNetworkString("Industrialization_UpdatePowerStatus")
        
        -- Optionally, you can handle server-side networking here
        -- For example:
        -- net.Send or net.Broadcast logic
    end
    
    if CLIENT then
        -- Table to store player inventory
        local PlayerInventoryUI = {}
    
        -- Receive inventory updates from the server
        net.Receive("Industrialization_UpdateInventory", function()
            local inventory = net.ReadTable()
            PlayerInventoryUI = inventory -- Utilize the 'inventory' variable
            -- Refresh the inventory UI if it's open
            if IsValid(vgui.GetControl("DFrame", "Industrialization_InventoryFrame")) then
                local frame = vgui.GetControl("DFrame", "Industrialization_InventoryFrame")
                local list = frame:GetChildren()[1] -- Assuming the DListView is the first child
                list:Clear()
                for resource, qty in pairs(PlayerInventoryUI) do
                    if qty > 0 then
                        list:AddLine(ResourceTypes[resource].name or resource, qty)
                    end
                end
            end
        end)
    
        -- Function to create and display the inventory UI
        function CreateInventoryUI()
            if IsValid(vgui.GetControl("DFrame", "Industrialization_InventoryFrame")) then return end
    
            local frame = vgui.Create("DFrame")
            frame:SetTitle("Inventory")
            frame:SetSize(400, 600)
            frame:Center()
            frame:MakePopup()
            frame:SetName("Industrialization_InventoryFrame")
    
            local list = vgui.Create("DListView", frame)
            list:Dock(FILL)
            list:AddColumn("Resource")
            list:AddColumn("Quantity")
    
            for resource, qty in pairs(PlayerInventoryUI) do
                if qty > 0 then
                    list:AddLine(ResourceTypes[resource].name or resource, qty)
                end
            end
        end
    
        -- Bind a key to open the inventory (e.g., F1)
        hook.Add("PlayerButtonDown", "Industrialization_OpenInventory", function(ply, button)
            if ply ~= LocalPlayer() then return end
            if button == KEY_F1 then
                CreateInventoryUI()
            end
        end)
    
        -- Factory Control Panel (example)
        function CreateFactoryControlPanel(factory)
            if not IsValid(factory) then return end
            if IsValid(vgui.GetControl("DFrame", "Industrialization_ControlPanelFrame")) then return end
    
            local frame = vgui.Create("DFrame")
            frame:SetTitle("Factory Control Panel")
            frame:SetSize(300, 200)
            frame:Center()
            frame:MakePopup()
            frame:SetName("Industrialization_ControlPanelFrame")
    
            local startButton = vgui.Create("DButton", frame)
            startButton:SetText("Start Processing")
            startButton:SetPos(50, 50)
            startButton:SetSize(200, 30)
            startButton.DoClick = function()
                factory:StartProcessing()
                frame:Close()
            end
    
            local stopButton = vgui.Create("DButton", frame)
            stopButton:SetText("Stop Processing")
            stopButton:SetPos(50, 100)
            stopButton:SetSize(200, 30)
            stopButton.DoClick = function()
                factory:StopProcessing()
                frame:Close()
            end
        end
    
        -- Similar functions can be created for Smelter, Assembly Machine, etc.
    end
    

    3. Explanation of Changes

    • Server-Side (if SERVER then):

      • Network Strings Registration: util.AddNetworkString calls are moved inside the if SERVER then block to ensure they are only executed on the server.
      • Server Logic: If you have any server-side networking or logic related to these network strings, it can also be placed here.
    • Client-Side (if CLIENT then):

      • UI and Networking: All client-side UI elements and network message receivers are kept inside the if CLIENT then block.
      • No Server Functions: Ensure that no server-only functions or variables are called or referenced here.

    4. Additional Recommendations

    • Separate Server and Client Files:

      • For better organization and to prevent similar issues in the future, consider separating server and client scripts into different files. For example:
        • ui_elements_server.lua for server-side code.
        • ui_elements_client.lua for client-side code.
      • Include them appropriately using if SERVER then include("ui_elements_server.lua") end and if CLIENT then include("ui_elements_client.lua") end.
    • Ensure Proper Inclusion:

      • Make sure that ui_elements.lua is included on both server and client if necessary. Typically, this is handled in your autorun or init scripts.
    • Check Network Message Usage:

      • Ensure that all net.Send, net.Broadcast, and related network functions are used correctly and correspond to the registered network strings.

    Final Thoughts

    The key takeaway is to clearly separate server-side and client-side code in your addons. Attempting to execute server-specific functions on the client will lead to errors like the one you've encountered. By restructuring your code as shown above, you should be able to eliminate the nil value error and ensure smooth communication between the client and server.

    If you continue to experience issues after these changes, consider checking other parts of your addon for similar server-client separation problems or provide more details for further assistance.