|
| 12 Jul 2015 10:55 PM |
if I want a part that checks the magnitude of every person in the server.
Should I use for i,v in pairs()
or
game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]?
I think the second choice is better because when I tried using for i,v in pairs the script broke when someone leaves the server. |
|
|
| Report Abuse |
|
|
iiEssence
|
  |
| Joined: 18 Jun 2014 |
| Total Posts: 3467 |
|
|
| 12 Jul 2015 10:58 PM |
for i,v in pairs(game.Players:GetChildren())
I don't need a siggy |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2015 11:06 PM |
Yeah what he said
The second one chooses a random player from players |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2015 11:06 PM |
This checks if the player is still there, if the player has a character, and if the character has a torso. Something important to notice is the order in which they are checked, because if one fails then because there is an and requirement the if statement fails without ever checking the next two which could be erroneous, because if player is nil then there's no way you could even check its non-existent character, the torso in such a case would be totally out of the question. You might need to check the case of character, it's been about two weeks since I've been in town and as a direct consequence my roblox lua is a bit rusty.
for _,player in pairs (game.Players:GetChildren ()) do if (player and player.character and player.character.Torso) then magnitude = (part.position-player.character.Torso.position).magnitude end end |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2015 12:12 PM |
Okay, but wouldn't for i,v in pairs() go in order?
so it'd go from
Player[1], then player[2], then player[3] and so on. And wouldn't it stop on the last player? or does it go back to player 1?
Because over night I was thinking maybe I should just do a for i loop checking how close every player is each second. |
|
|
| Report Abuse |
|
|
lolb3
|
  |
| Joined: 16 Jan 2010 |
| Total Posts: 2268 |
|
|
| 13 Jul 2015 12:21 PM |
while true do in pairs loop end will end at the last player then restart wait end |
|
|
| Report Abuse |
|
|
AstroCode
|
  |
| Joined: 09 Oct 2009 |
| Total Posts: 1122 |
|
|
| 13 Jul 2015 12:24 PM |
while wait() do for i, v in pairs(game.Players:GetPlayers()) do if v.Character then -- do your magnitude stuff end end end |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2015 05:38 PM |
Guys, I did:
if v.Character and if v then, etc. But it still breaks when they leave, why? |
|
|
| Report Abuse |
|
|