How to know how many players have X thing

Hello, i am making a game and i am having problems with a script, i wanna know how to check how many players have lost

I am doing a script that is supposed to do this: When the part is touched by player, the script is meant to check how many players have lost.

the player haves a folder inside called “results”
and inside the folder there’s two bool values called “winned” and “lost”
the script is meant to check if the lost value is true
but this is the part i dont know how to do

then it makes two number values, “PlayerNumber”, a rendom number selected for player. (mininum value = 0, maximum = how many players have lost)
and “eliminationValue” that’s the same but it is on replicated storage.

the game prints what is the value of both of them.

if the eliminationValue.Value and the PlayerNumber are the same, the script is meant to kick you but but i didn’t put that in yet.

i think that is script is kinda messy, how can i fix the problem i had with the eight line of code?


local player = game.Players.LocalPlayer

local results = player:WaitForChild("Results")

game.Workspace.Part.Touched:Connect(function()
	
	local PlayerLosed = ? --The thing i can't figure out
	
	local PlayerNumber = Instance.new("NumberValue")
	PlayerNumber.Value = math.random(0, PlayerLosed)
	PlayerNumber.Parent = game.Players.LocalPlayer
	
	wait(0.5)
	
	print("The player number is"..PlayerNumber.Value.."")
	
	local EliminationValue = Instance.new("NumberValue")
	EliminationValue.Value = math.random(0, PlayerLosed)
	EliminationValue.Parent = game.ReplicatedStorage
	
	wait(0.5)
	
	print("The eliminate number is"..EliminationValue.Value.."")
	
	if EliminationValue == PlayerNumber then
		--I will put something here later, you dont need to fill it.
	end
end)

thanks for the help!

You can iterate through every player in the game and add up a number variable for every lost player:

local PlayersLost = 0
for _, player in pairs(game.Players:GetPlayers()) do
   if player:FindFirstChild("Results") then
      if player.Results.lost.Value == true then
         PlayersLost = PlayersLost + 1
      end
   end
end
1 Like