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.

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.

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

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:

  • 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