• Tables - attempt to index a nil value
    9 replies, posted
[lua]util.AddNetworkString( "holsteredweps" ) local function sendholstered() local Table = {} for _, holstpl in pairs(player.GetAll()) do if !IsValid(holstpl) or !holstpl:Alive() or holstpl:IsSpec() then return end for i, v in pairs(holstpl:GetWeapons()) do Table.holstpl.i = v end Table.holstpl.Ges = (#holstpl.GetWeapons()) end net.Start( "holsteredweps" ) net.WriteTable( Table ) net.Broadcast() print (Table) -- For me. Testing. Not important for this issue :D end hook.Add("Think","HG_Think_Srv",sendholstered)[/lua] Since Clients can't read out the weapons of other clients i need to send them from the server. The following error occurs though and i can't get the hold of what i am doing wrong. [lua]L 03/27/2017 - 17:31:28: Lua Error: [ERROR] addons/holstered_weps/lua/autorun/server/sv_holster.lua:9: attempt to index field 'holstpl' (a nil value) 1. fn - addons/holstered_weps/lua/autorun/server/sv_holster.lua:9 2. unknown - addons/ulib/lua/ulib/shared/hook.lua:110[/lua] I also tried doing Table[holstpl][i] for example > Didn't work. I should work right? I mean holstpl is not nil or anything. He goes through every player! Edit: Moved local Table outside of function just to be sure. Nothing :( [B]If possible don't give the answer right away just give a tip at what i should look at :P[/B]
As Max said, Table.holstpl.i is the same as Table["holstpl"].i or Table["holstpl"]["i]. But Table["holstpl"] is undefined. Simply adding [B]Table.holstpl = {}[/B] wouldn't fix the issue, because you need a table for each player. I'd use their SteamID as the key But why are you looping through all players in a Think hook, and then sending a net message, let alone a table? That is extremely inefficient and there's definitely a better way to do it Also seeing as this is probably TTT - [B]!holstpl:Alive() or holstpl:IsSpec()[/B] can be shortened to [B]not holstpl:IsTerror()[/B]
I think that happens because you're trying to assign a value to Table.holstpl.i, but since the table is empty, the field "holstpl" doesn't exist yet. Try to create another table as Table.holstpl, and then fill the fields.
Done. [lua]util.AddNetworkString( "holsteredweps" ) local Table = {} local function sendholstered() --Table.pl = {} for _, pl in pairs(player.GetAll()) do if !IsValid(pl) or !pl:Alive() or pl:IsSpec() then return end Table[pl] = {} --Table[pl][i] = {} --Table.pl = pl for i, v in pairs(pl:GetWeapons()) do Table[pl][i] = v end --Table[pl]["Ges"] = (#pl:GetWeapons()) end net.Start( "holsteredweps" ) net.WriteTable(Table) net.Broadcast() PrintTable(Table) end hook.Add("Think","HG_Think_Srv",sendholstered)[/lua] Thanks for all your support!
[QUOTE=Zmeja;52019275]Done. [lua]util.AddNetworkString( "holsteredweps" ) local Table = {} local function sendholstered() --Table.pl = {} for _, pl in pairs(player.GetAll()) do if !IsValid(pl) or !pl:Alive() or pl:IsSpec() then return end Table[pl] = {} --Table[pl][i] = {} --Table.pl = pl for i, v in pairs(pl:GetWeapons()) do Table[pl][i] = v end --Table[pl]["Ges"] = (#pl:GetWeapons()) end net.Start( "holsteredweps" ) net.WriteTable(Table) net.Broadcast() PrintTable(Table) end hook.Add("Think","HG_Think_Srv",sendholstered)[/lua] Thanks for all your support![/QUOTE] You're looping through every player and sending them a table every tick, thats not a good idea.
[QUOTE=Nick78111;52019805]You're looping through every player and sending them a table every tick, thats not a good idea.[/QUOTE] Adding to this, you're sending a table via net message every tick. Your server is probably praying for mercy. But [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/GetWeapons]Player:GetWeapons[/url] is shared, so this can all be done on the client
[QUOTE=JasonMan34;52019897]Adding to this, you're sending a table via net message every tick. Your server is probably praying for mercy. But [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/GetWeapons]Player:GetWeapons[/url] is shared, so this can all be done on the client[/QUOTE] Player:GetWeapons is not shared [B]in TTT[/B], thats why i started this whole thing :D Also i hooked it to "Player switch weapons" so its not called every tick :3
Is there a hook for a player picking up a weapon? Because player switching weapons is not getting called when a player picks up a weapon. Otherwise after two days of testing everything works just fine so thanks all for your support! :)
[QUOTE=Zmeja;52036884]Is there a hook for a player picking up a weapon? Because player switching weapons is not getting called when a player picks up a weapon. Otherwise after two days of testing everything works just fine so thanks all for your support! :)[/QUOTE] Learn how to use the G-Mod wiki. It's an extremely useful tool that any coder relies heavily on. This could help you [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/WeaponEquip]GM:WeaponEquip[/url]
[QUOTE=JasonMan34;52036897]Learn how to use the G-Mod wiki. It's an extremely useful tool that any coder relies heavily on. This could help you [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/WeaponEquip]GM:WeaponEquip[/url][/QUOTE] Thanks! I searched at the wiki but i was searchng for "OnPickup" and identical phrases :D Who would have guessed that it is Equip ;D Anyway thanks again!
Sorry, you need to Log In to post a reply to this thread.