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

How to count number of children in a folder?

Asked by
Dekishi 12
4 years ago

Making a last man standing game mode. i have a folder in the workspace that stores players called "Ingame". I want to end the game when the folder reaches a number of one. How do I constantly check the value of the folder?

2 answers

Log in to vote
1
Answered by 4 years ago

Though the above is correct, there are even better ways of doing such. To get player count, one can just use one line of code

#game.Players:GetChildren() --This returns the number of players

Additionally, one can further streamline it by turning the while wait() do into a function triggered by an event.

game.Players.PlayerAdded:Connect(function(plr)
    if #game.Players:GetChildren() <= 1 then
        --Code to end the game.
    end
end)

I hope this helped, if you have any comments or suggestions, please comment down below!

Ad
Log in to vote
0
Answered by 4 years ago

Actually pretty easy: just create an array with the children and get the lengh of the array like this:

local count = 0
for _,v in pairs(game:GetService("Players"):GetPlayers) do
    count = count + 1
end

with the loop applied it would look like this:

local playerarray = --whatever you store the player is idk

local playercount = 0
while wait() do --the wait() is for the while loop to not timeout
    for i, player in pairs(playerarray) do
        playercount = playercount + 1
    end
    --now the check if the player count is 0
    if playercount <= 1 then
        --run code
    end
end

Answer this question