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

Get all players?

Asked by 11 years ago

How would I make the script go through all the players, and effect all of them? (Global script in the Workspace) Here's two things I tried:

01for I,v in pairs(game.Players:GetPlayers()) do
02local randomCoor = coordinates[math.random(#coordinates)]
03local knife = game.ServerStorage.Knife:Clone()
04knife.Parent = v:WaitForChild("Backpack")
05game.Workspace.lobby.timer.SurfaceGui.timelft.Text = "Round In Progress..."
06c = v.Character or v.CharacterAdded:wait()
07c.Torso.CFrame = CFrame.new(randomCoor)
08wait(60)
09for I,v in pairs(game.Players:GetPlayers()) do
10c = v.CharacterAdded:wait()
11v.Character.Humanoid.Health = 0
12end
13end

Those didn't effect all players though, it only effected 1 player at random, for both. This is just a cutout of my script, but please help with

2 answers

Log in to vote
0
Answered by
Dom2d2 35
11 years ago

Simple, let's say you want to damage every player in the game by half their health. You could say this:

1for i, v in pairs(game.Players:GetChildren()) do -- i is what player the for loop is currently on, the v is the payer itself.
2 
3--Here in the loop, you can say whatever you want to happen to everybody.
4v.Character.Humanoid.Health = 50
5 
6end
Ad
Log in to vote
0
Answered by
AxeOfMen 434 Moderation Voter
11 years ago

You need to create a clone of the knife and give the clone to each player, otherwise the knife will not be in ServerStorage for the next iteration over the players:

game.ServerStorage.Knife:Clone().Parent = v:WaitForChild("Backpack")

You are also waiting for 60 seconds for each player before then killing each player. It is unusual that you are iterating over the players inside a loop that is iterating over all the players. I can't tell what your intentions are there. If you tell me what you want it to do, I might be able to help further.

Answer this question