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

Whats the word for all players?

Asked by 9 years ago

Like in , " game.Workspace.Player1.Humanoid.MaxHealth = 0" How do I change it to work on all the players?

0
game.Players:GetChildren() Timster111 25 — 9y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

So, you're trying to kill all the players? This reuires you you iterate through every decendant of game.Players, using either the GetChildren or GetPlayers methods. I preferr to use GetPlayers, just in case there's a model in game.Players for some reason.

To iterate through each of the players we need to use a pairs loop

for i,v in pairs(game.Players:GetPlayers()) do

Now that we have the loop defined, what are we trying to accomplish. We want to kill every player. We could with use BreakJoints on the curent index's character, or set the current index's character's humanoid's health to 0. I prefer BreakJoints because it's quicker(: The current index is the player, since we're iterating through a player table, each player is called an index in the table. We'll use 'v' to define our current index, because we defined it like so in the loop .

v.Character:BreakJoints()

So now lets put it together.

for i,v in pairs(game.Players:GetPlayers()) do
    v.Character:BreakJoints()
end
Ad
Log in to vote
0
Answered by 9 years ago

Changing the property of all players is not as simple as a single line of code. To achieve this affect, you have to use a generic for loop to execute code for each child of Players (AKA for each Player).

for _, player in pairs(game.Players:GetChildren()) do --Grabs a table of all the Players in the game
    player.Humanoid.MaxHealth = 0 --Changes each Player's MaxHealth
end

For an explanation of generic for loops, refer to this article on the Roblox Wiki. Just ask any questions if you're confused. I hope this helps, and please upvote and accept if you found my answer helpful.

0
Recheck your code; If this code is attempted, it will cause an error [Line 2], also, to me, it is better to use the 'GetPlayers' method, 'GetPlayers' creates a table of the current Players in 'game.Players' [game.Players:GetPlayers()]. TheeDeathCaster 2368 — 9y

Answer this question