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

Counting true values within a table?

Asked by 9 years ago

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

Log in to vote
1
Answered by
DataStore 530 Moderation Voter
9 years ago

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.

0
Does this remove the player from the table once they leave the game? peoplemove12 148 — 9y
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. DataStore 530 — 9y
0
Okay. Thanks so much! peoplemove12 148 — 9y
Ad

Answer this question