Per-Player tables that *don't* save across map changes

[lua]local choice = vgui.Create(“DComboBox”,choicepanel)
choicep:SetSize(125,25)
choicep:SetPos(62.5,40)
choicep:SetValue(“Primary”)
choicep:AddChoice(“M4A1”)
choicep:AddChoice(“AK-47”)
choicep:AddChoice(“G36C”)
choicep:SetContentAlignment(5)
choicep.OnSelect = function(index, value, data)
RunConsoleCommand(“loadout_primary”,data)
end[/lua]
What I wanna do with that code is when the player picks a selection it will run the console command. It’s a serverside concommand. Then it would use the ‘data’ on spawn (it’d be converted to actual weapon names obviously) I’ve been using this code for a while:
[lua]concommand.Add(“loadout”, function(ply, cmd, args)
loadout[1]=args[1]
loadout[2]=args[2]
loadout[3]=args[3]
loadout[4]=args[4]
end)[/lua]
It’s serverside (again) but I didn’t realize for awhile that the problem is it’s not per-player. I don’t want to use metatables, they’d be messy for this type of thing and I do not want to save when it changes maps/restarts the server/they rejoin. I could use networking or something but then it would have to be saved in a table and loaded in the spawn (unless the player has to select them every spawn which is a no-no), which again would require per-player tables. How would I get this to work?

Attach the table to the player object
Also, since you’re basically just copying the table just point to it
[lua]concommand.Add(“loadout”, function(ply, cmd, args)
ply.Loadout = args
end)[/lua]
However, I really reccommend that you do some server side checking to ensure that the player is not doing things they shouldn’t be.

How can I ‘attach’ a table to something?

Exactly as my example shows. Using “myTable.Something = whatever” is exactly the same as
[lua]myTable[ “Something” ] = whatever[/lua]
Because a player “object” is pretty much a table with a bunch of fancy things in it. The player object is maintained as long as the player stays in the server (they reconnect when the map changes - wiping the object) so whenever you get passed a player object in a hook etc. the things you attatched to it beforehand will still be there.

[lua]Loadout = {}

concommand.Add(“loadout_attachment”, function(ply, cmd, args)
ply.Loadout[1] = args
end)
concommand.Add(“loadout_sight”, function(ply, cmd, args)
ply.Loadout[2] = args
end)[/lua]

Typed this in console:


loadout_attachment suppressor

(Note I am using FA:S 2 Alpha SWEPs, suppressor is the codename of the attachment and I have tested this using suppressor alone.)

In spawn function:
[lua]ply:FAS2_PickUpAttachment(ply.Loadout[1])
ply:FAS2_PickUpAttachment(ply.Loadout[2])[/lua]

I don’t get the attachments. And putting if I put THIS in the spawn function:
[lua]ply:FAS2_PickUpAttachment(“suppressor”)
ply:FAS2_PickUpAttachment(“compm4”)[/lua]
It would have worked.

Did you check the contents of ply.Loadout?
It’s pretty obvious that anything in that table will itself be a table because the “args” parameter in the concommand.Add callback is a table of arguments given by the player. If you want to do it this way, you should use args[1] in both instead.
[lua]concommand.Add(“loadout_attachment”, function(ply, cmd, args)
ply.Loadout[1] = args[1]
end)
concommand.Add(“loadout_sight”, function(ply, cmd, args)
ply.Loadout[2] = args[1]
end)[/lua]
Also I have no idea how you haven’t got an “attempt to index a nil value” error from that
[lua]Loadout = {}[/lua]
That’s wrong. Remember, you want to attatch it to the player object, ply, so you’ll need to do something like this is both concommand.Add()s
[lua]concommand.Add(“loadout_attachment”, function(ply, cmd, args)
ply.Loadout = ply.Loadout or {} – You don’t want to override the table each time - only create the table if ply.Loadout is nil
ply.Loadout[1] = args[1]
end)
concommand.Add(“loadout_sight”, function(ply, cmd, args)
ply.Loadout = ply.Loadout or {}
ply.Loadout[2] = args[1]
end)[/lua]

Used your new code, and removed “Loadout = {}”, yet this line:
[lua]ply:FAS2_PickUpAttachment(ply.Loadout[1])[/lua]
Gets an error that says Loadout is a nil value. And I did type the console commands with the correct args for loadout_sight and loadout_attachment

[editline]edit[/editline]
It’s fixed after typing the loadout_sight/loadout_attachment again. However, I don’t get my attachments, even if replacing the exact arguments with the string (like in my last post) would’ve worked.

[editline]edit2[/editline]
[lua]print(ply.Loadout[1])[/lua]
Made it print ply.Loadout[1], it’s nil for some reason even though the concommand was entered by me in-game.

[editline]lol[/editline]
Nevermind, I’m dumb sometimes.