[gmod-industrialization] addons/gmod-industrialization/lua/modules/uielements.lua:4:…
- User
[gmod-industrialization] addons/gmod-industrialization/lua/modules/ui_elements.lua:4: attempt to call field 'AddNetworkString' (a nil value)
- unknown - addons/gmod-industrialization/lua/modules/ui_elements.lua:4
- 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 - Assistant
The error you're encountering stems from attempting to call
util.AddNetworkStringon the client-side, where this function is undefined. In Garry's Mod,util.AddNetworkStringis a server-side function used to register network messages that clients and servers can communicate with. Calling it on the client side results in thenil valueerror 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.AddNetworkStringCall: Theutil.AddNetworkStringfunction is being called within anif CLIENT thenblock, which means it's executing on the client side. - Client vs. Server Scope:
util.AddNetworkStringis only available on the server. On the client side, this function doesn't exist, hence thenil valueerror.
Solution
1. Separate Server and Client Code
You need to ensure that server-side functions like
util.AddNetworkStringare only executed on the server, and client-side functions are only executed on the client.2. Modify
ui_elements.luaHere's how you can restructure your
ui_elements.luato 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. end3. Explanation of Changes
-
Server-Side (
if SERVER then):- Network Strings Registration:
util.AddNetworkStringcalls are moved inside theif SERVER thenblock 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.
- Network Strings Registration:
-
Client-Side (
if CLIENT then):- UI and Networking: All client-side UI elements and network message receivers are kept inside the
if CLIENT thenblock. - No Server Functions: Ensure that no server-only functions or variables are called or referenced here.
- UI and Networking: All client-side UI elements and network message receivers are kept inside the
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.luafor server-side code.ui_elements_client.luafor client-side code.
- Include them appropriately using
if SERVER then include("ui_elements_server.lua") endandif CLIENT then include("ui_elements_client.lua") end.
- For better organization and to prevent similar issues in the future, consider separating server and client scripts into different files. For example:
-
Ensure Proper Inclusion:
- Make sure that
ui_elements.luais included on both server and client if necessary. Typically, this is handled in yourautorunorinitscripts.
- Make sure that
-
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.
- Ensure that all
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 valueerror 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.
- Location of