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

If I wanted to include all players in a workspace in a script, what would say?

Asked by 9 years ago

Would I say: Workspace.Players ?

0
What are you trying to accomplish by including all players? funyun 958 — 9y
0
I'm trying to remove their hats. secretboy6 68 — 9y
0
Touching a "part" takes off a specific hat they're wearing. secretboy6 68 — 9y

2 answers

Log in to vote
3
Answered by
funyun 958 Moderation Voter
9 years ago

Oki, let's make a function that we can use in a script to remove all the players' hats.

First, you declare the function.

1function RemoveHats()
2 
3end

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.

1function RemoveHats()
2    local players = game.Players:GetPlayers()
3end

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.

1function RemoveHats()
2    local players = game.Players:GetPlayers()
3 
4    for _, player in pairs(players) do
5 
6    end
7end

Now let's go through all the stuff in that guy's character model, again with a generic for loop.

1function 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
9end

Let's see if we can find a hat or two.

01function 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
11end

If we do, we'll destroy it.

01function 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
11end

And that is our final product. You can paste that in a script and then call it whenever you'd like.

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

In Lua, a single variable has a single object attached to it.

Thus, while you can say

1who = workspace.BlueTaslem
2who.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):

1local players = game.Players:GetPlayers()
2 
3for _, 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
9end

We needed a little extra machinery:

  • While I can say workspace.BlueTaslem easily because there's only one of me, there's a lot of other non-player things in the workspace
  • There are only players in the game.Players service...
  • but those are different from the models in the workspace. We get the models in the Workspace using .Character...
  • but that can be nil,
  • and when it's not, they don't necessarily have all of their parts (so we have to check using :FindFirstChild

  1. You can, but it would be very confusing and a pain to do. 

1
gg bluetaslem funyun 958 — 9y

Answer this question