I'm fairly novice when it comes to lua, really trying to get out of my comfort zone. As of now I was giving a command (coded under server) that could set a players health. I could probably do it were it not for me having no clue how to use the
[CODE]for k, v in pairs[/CODE]
loop.
I've no clue what to do with it or where to put it so I thought I'd put this account I made a while back to good use
here's the code I have so far.
Like I said, novice, be gentle lmao
[CODE]function SetHealthCommand(ply, text, team)
local Argument = string.Explode(" ", text)
if (Argument[1] == "!sethealth") then
if (Argument[2] == text) then
if (tonumber (Argument[3]) then
for k, v in pairs (player.GetAll()) do
v:Argument[2]
v:SetHealth() == Argument[3]
end
end
end
return ""
end
end
hook.Add("PlayerSay" "Command_/sethealth" SetHealthCommand)[/CODE]
edit: Looking at it now it seems I screwed up on argument[2], gonna fix it.
You use it when you're looping over a table. Whatever is in the pairs' function parentheses is the table that is going to be looped over.
Every value in a table has a key associated with it. k and v are short for [B]k[/B]ey and [B]v[/B]alue. In this loop, the key will be k and the value will be v.
[B]for blah, blahblah in pairs[/B] is the same as [B]for k, v in pairs[/B]. They are just variable names. You can name them something else if you wish.
If you have a table like this, for example
[lua]
local tab = {
["key"] = "value"
}
for k, v in pairs(tab) do
print(k, v)
end[/lua]
k will be "key" and v will be "value"
Sorry, you need to Log In to post a reply to this thread.