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.
function RemoveHats() 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.
function RemoveHats() local players = game.Players:GetPlayers() 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.
function RemoveHats() local players = game.Players:GetPlayers() for _, player in pairs(players) do end end
Now let's go through all the stuff in that guy's character model, again with a generic for loop.
function RemoveHats() local players = game.Players:GetPlayers() for _, player in pairs(players) do for _, thing in pairs(player.Character:GetChildren()) do end end end
Let's see if we can find a hat or two.
function RemoveHats() local players = game.Players:GetPlayers() for _, player in pairs(players) do for _, thing in pairs(player.Character:GetChildren()) do if thing:IsA("Hat") then end end end end
If we do, we'll destroy it.
function RemoveHats() local players = game.Players:GetPlayers() for _, player in pairs(players) do for _, thing in pairs(player.Character:GetChildren()) do if thing:IsA("Hat") then thing:Destroy() end end end 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
who = workspace.BlueTaslem 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):
local players = game.Players:GetPlayers() for _, player in pairs( players ) do local character = player.Character if character and character:FindFirstChild("Humanoid") then -- Use the same "meat" we did before: character.Humanoid.Health = 0 end 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. ↩