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

Counting true values within a table?

How could it be so if more than two people have a certain BoolValue (Playing) true then the game starts.

if #PlayersTable >= 2 and (checking if more than two players have a true value) then

1 answer

✓ Accepted answer

One way that you could achieve this is by iterating through the Players service, finding out who has their BoolValue (Playing) set to true, add them to a table and then have a function return the table for you to use.

E.g.

function GetPlayers()
	local Playing = {}
	for _, Player in pairs(game.Players:GetPlayers()) do
		if Player:FindFirstChild("Playing") and Player.Playing.Value then
			Playing[#Playing +1] = Player
		end
	end
	return Playing
end

local Playing = GetPlayers()
if #Playing >= 2 then 
	print("More than two players have set their Playing BoolValue to true!")
	for _, Player in pairs(Playing) do
		if Player.Character then
			Player.Character:MoveTo(Vector3.new(0, 200, 0)) 
		end
	end
end

You could also use the table being returned by the 'GetPlayers' function to teleport the users, etcetera, without the need of iterating through the entire Players service again. An example of this can be found between lines 14 and 18.

If you need anything explained further, just leave a comment to this reply.

3 comments

0

Does this remove the player from the table once they leave the game?

0

A player who has left won't count if they leave before the 'GetPlayers' function is ran. If they leave after, they'll still be in the returned table until the function is ran again.

0

Okay. Thanks so much!

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