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.

1function onePersonLeft()
2    for i, player in pairs(game.Players:GetPlayers()) do
3        if #player.Playing.Value == 1 then return true
4        else return false
5    end
6    end
7end
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:

1if player.Playing.Value then

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

1local count = 0
2for _, player in pairs( game.Players:GetPlayers() ) do
3    if player.Playing.Value then
4        count = count + 1
5    end
6end
7 
8-- count is how many people are playing

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

01local playing = {}
02for _, player in pairs( game.Players:GetPlayers() ) do
03    if player.Playing.Value then
04        table.insert(playing, player) -- add `player` to the list of `playing` people
05    end
06end
07 
08-- #playing is how many people are playing.
09-- playing[1], playing[2], ..., playing[#playing] are the people playing
10-- (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