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

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

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 comment

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.

1 answer

✓ Accepted answer

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)

2 comments

0

Oh, I misinterpreted what he wanted; all I did was fix what caused it to error. Thanks for the shout out though xD.

0

Thanks! This is pretty much exactly what I needed.

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