|
| 20 Jan 2016 05:12 PM |
for _,v in pairs(game.Players:GetChildren()) do if not v.Backpack:FindFirstChild("HyperLaserGun") then h = rps:WaitForChild("HyperLaserGun") -- whatever name it is h:Clone() h.Parent = v.Backpack end end
Can someone explain to me why v is referred to the player? |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 05:13 PM |
because this type of loop is like looping through a table of players, where each value, v, is a player.
|
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 05:15 PM |
for _,player in pairs(game.Players:GetPlayers()) do if not player.Backpack:FindFirstChild("HyperLaserGun") then local h = rps:WaitForChild("HyperLaserGun"):clone() h.Parent = player.Backpack end end |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 05:19 PM |
Oh right, the explanation. Basically, the pairs function constructs an iterator function, which when called, returns the next key/value pair in the table, or nil if it reached the end of the table.
The for loop continues to call the iterator, and runs a loop with those variables until it iterates through each key/value in the table. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 07:41 PM |
| OMY GOD OH MY GOD ITS CLONETROOPER HE RESPONDED TO MY POST HEY CLONE WASUUUUPPPPPPP!!!!!!!!! |
|
|
| Report Abuse |
|
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
|
| 20 Jan 2016 07:50 PM |
| The ROBLOX Wiki Staff consists of strong material. They help people. They will not respond to your non-sense, swim. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 07:52 PM |
| so clone v is like the "value" so what your saying is v can be anything? For example, I could technically do for _, bananas in pairs(game.Players:GetPlayers())? |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 07:53 PM |
"OMY GOD OH MY GOD ITS CLONETROOPER HE RESPONDED TO MY POST HEY CLONE WASUUUUPPPPPPP!!!!!!!!!"
I cringed |
|
|
| Report Abuse |
|
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
| |
|
62GB
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 4157 |
|
|
| 20 Jan 2016 07:59 PM |
| 'v' is just the variable that holds the value in the iteration. It can be anything. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2016 08:01 PM |
v is just a variable name. '_' is also just a variable name. It's usually 'i' for 'index'.
local tab = {1, 1, 2, 3, 5, 8, 13, 42}
for index, value in ipairs(tab) do print(index, value) end
outputs
1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 42
as you can see, the first thing that is printed is the current index. the second thing that is printed is value. value is equal to tab[index] |
|
|
| Report Abuse |
|
|
| |
|