• Looking to make a table of sorts
    9 replies, posted
I'm trying to puzzle out how exactly to make a table of player names, and how to add a number to each player when they, say, kill an NPC. The end goal here is to alert them every five kills with something like "playername, you have X kills." I understand how to alert them, and how to check if they've killed something, but its the table thats giving me trouble. Any help would be appreciated.
uhh something like [lua] hook.Add( "DoPlayerDeath", "derp", function( _, ply ) if ply:Frags() % 5 == 0 then ply:PrintMessage( HUD_PRINTTALK, "You just killed 5 people or something!" ) end end ) [/lua]
[QUOTE=EvacX;41507449]uhh something like [lua] hook.Add( "DoPlayerDeath", "derp", function( _, ply ) if ply:Frags() % 5 == 0 then ply:PrintMessage( HUD_PRINTTALK, "You just killed 5 people or something!" ) end end ) [/lua][/QUOTE] This I get. The trouble is creating a table to keep track of each players individual kills
Why do you need to do that?
[QUOTE=EvacX;41507507]Why do you need to do that?[/QUOTE] In my case, I'm building a very basic RPG script as a learning piece. I have one working, but as the XP and level are not per player, it is pretty broken.
No I mean why do you think that's neccessary? The ":Frags()" method already keeps track of how many kills a player has (assuming you're deriving from the 'base' gamemode).
[QUOTE=EvacX;41507608]No I mean why do you think that's neccessary? The ":Frags()" method already keeps track of how many kills a player has (assuming you're deriving from the 'base' gamemode).[/QUOTE] I want to store their level, XP, and name in one place for ease of access
So store their player object then?
Maybe? I'm just getting started here
I haven't worked with lua or tables in awhile, but something like this might work. [CODE] local playershit = { {name= "Sasha", kills = 1337, deaths = 0, XP = 9001}, {name= "Oubliette", kills = -99999999999, deaths = 999999999999, XP = 0}, } [/CODE] Then just use table.Insert to place in new entries. I think you can also do something like this with the columns if you would prefer this method. [CODE] local playershit = { Oubliette = {kills = -99999999999, deaths = 999999999999, XP = 0} } [/CODE] Access data members in a for loop for my above solution, or access that bottommost piece like this. [CODE] playershit.Oubliette.kills [/CODE] [editline]18th July 2013[/editline] Also I was bored, here's the for loop you would use to iterate through that table. [CODE] for k v in pairs(playershit) do if (v.name == "yournamehere") then v.kills = 1/0; v.deaths = 700; //etc end end [/CODE]
Sorry, you need to Log In to post a reply to this thread.