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

How do I get the number of players with a certain value?

Asked by 9 years ago

So basically, each person that joins the game gets an IntValue named "Playing" spawned inside them set to 0. Well, when my minigame thing initiates, everyone's 'Playing' value gets set to 1. Then when they die, it gets set back to 0.

What I need help figuring out is how would I find the amount of people who have their 'Playing' value set to 1? Below, you can see what I tried doing, but the third line isn't valid.

function onePersonLeft()
    for i, player in pairs(game.Players:GetPlayers()) do
        if #player.Playing.Value == 1 then return true
        else return false
    end
    end
end
1
Remove the #; the # gives the number of objects in a table such as game.Players:GetPlayers(), but for i, player in pairs returns player as one of the values of that table. By the way, couldn't you just use a BoolValue for this? It would accomplish the same thing except with true/false instead of 1/0. Legojoker 345 — 9y

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First, I agree with Legojoker: If you have a boolean value (is/is not) -- use a BoolValue!

Assuming you're using a BoolValue, you check if player is currently playing like this:

if player.Playing.Value then

So we just need to find out for how many different players this is true:

local count = 0
for _, player in pairs( game.Players:GetPlayers() ) do
    if player.Playing.Value then
        count = count + 1
    end
end

-- count is how many people are playing

If you care who (which players) are playing, instead of counting you can make a list:

local playing = {}
for _, player in pairs( game.Players:GetPlayers() ) do
    if player.Playing.Value then
        table.insert(playing, player) -- add `player` to the list of `playing` people
    end
end

-- #playing is how many people are playing.
-- playing[1], playing[2], ..., playing[#playing] are the people playing
-- (in no particular order)
0
Oh, I misinterpreted what he wanted; all I did was fix what caused it to error. Thanks for the shout out though xD. Legojoker 345 — 9y
0
Thanks! This is pretty much exactly what I needed. megamario640 50 — 9y
Ad

Answer this question