Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use for i,v in pairs(game.Players:GetPlayers()) do?

Asked by
wjs3456 90
9 years ago

I have a relatively basic question on doing for i,v in pairs(game.Players:GetPlayers()) do. (I suppose there are also similar methods like it to accessing the players but this is what I'm using) So basically I was wondering if you used

for i,v in pairs(game.Players:GetPlayers()) do
    if v.TeamColor == BrickColor.new("Bright blue") then
    v.Character.Humanoid.Health = 0

Would all the players have to be blue for it to work or would it just kill those who were on the blue team? Hope this makes sense. Thanks

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

for is a loop. That means it is done separately for each item gone over in the loop -- in this case, it is each player.

So this executes for each player separately. If each player (v) happens to be blue team, then it will attempt to kill them -- which means you just kill all of blue team.

The loop will examine non-blue players, but simply ignore them. (if you had place a break in an else clause, though, then it would start to act funny -- but that is not the default behavior that you fear)


A warning that this could error and stop working: v.Character can be nil (when the character hasn't spawned yet) which would cause an error when you try to access its .Humanoid. You should also check v.Character exists, alongside checking the TeamColor.

0
Cool thank you. wjs3456 90 — 9y
Ad

Answer this question