Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to count number of children in a folder?

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

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!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.