Would I say: Workspace.Players ?
Oki, let's make a function that we can use in a script to remove all the players' hats.
First, you declare the function.
1 | function RemoveHats() |
2 |
3 | end |
Next, let's get all the players. To do this, we do game.Players:GetPlayers()
. What this does is it returns an array of the players currently in the game. If you don't know what an array or a table is, please look it up on the wiki before you ask us.
1 | function RemoveHats() |
2 | local players = game.Players:GetPlayers() |
3 | end |
Now, we start up a generic for loop. Basically, what this does is it goes through all the stuff in the table/array and does something with each element in it. Again, ask the wiki first if you don't know this stuff.
1 | function RemoveHats() |
2 | local players = game.Players:GetPlayers() |
3 |
4 | for _, player in pairs (players) do |
5 |
6 | end |
7 | end |
Now let's go through all the stuff in that guy's character model, again with a generic for loop.
1 | function RemoveHats() |
2 | local players = game.Players:GetPlayers() |
3 |
4 | for _, player in pairs (players) do |
5 | for _, thing in pairs (player.Character:GetChildren()) do |
6 |
7 | end |
8 | end |
9 | end |
Let's see if we can find a hat or two.
01 | function RemoveHats() |
02 | local players = game.Players:GetPlayers() |
03 |
04 | for _, player in pairs (players) do |
05 | for _, thing in pairs (player.Character:GetChildren()) do |
06 | if thing:IsA( "Hat" ) then |
07 |
08 | end |
09 | end |
10 | end |
11 | end |
If we do, we'll destroy it.
01 | function RemoveHats() |
02 | local players = game.Players:GetPlayers() |
03 |
04 | for _, player in pairs (players) do |
05 | for _, thing in pairs (player.Character:GetChildren()) do |
06 | if thing:IsA( "Hat" ) then |
07 | thing:Destroy() |
08 | end |
09 | end |
10 | end |
11 | end |
And that is our final product. You can paste that in a script and then call it whenever you'd like.
In Lua, a single variable has a single object attached to it.
Thus, while you can say
1 | who = workspace.BlueTaslem |
2 | who.Humanoid.Health = 0 |
There's no way1 to set who
so that it affects all players -- there isn't one value meaning all the players together.
There is a value which is the list of all players. While the list doesn't have a humanoid (lists aren't alive), each element of the list does.
This is where we use a for
loop (search for more information about them):
1 | local players = game.Players:GetPlayers() |
2 |
3 | for _, player in pairs ( players ) do |
4 | local character = player.Character |
5 | if character and character:FindFirstChild( "Humanoid" ) then |
6 | -- Use the same "meat" we did before: |
7 | character.Humanoid.Health = 0 |
8 | end |
9 | end |
We needed a little extra machinery:
workspace.BlueTaslem
easily because there's only one of me, there's a lot of other non-player things in the workspacegame.Players
service....Character
...nil
,:FindFirstChild
You can, but it would be very confusing and a pain to do. ↩