How do I check if a player is still in a round?

I’m creating a script at the moment that runs a round based system, once players are teleported into the round using a random spawn they get a value in their character with the name “inGame”.

I want to constantly check how many players have this tag, and end the game when there is only 1 person left with the tag, how would I do this?

My code is:

local status = game:GetService("ReplicatedStorage"):WaitForChild("Status")

while true do
	status.Value = "Intermission"
	wait(10)
	status.Value = "In Game"
	for i,v in pairs(game.Players:GetPlayers()) do
		local toTP = math.random(1,3)
		v.Character.HumanoidRootPart.CFrame = workspace.System[toTP].CFrame
		local gameTag = Instance.new("BoolValue")
		gameTag.Name = "inGame"
		gameTag.Parent = v.Character
	end
end

You could make a folder somewhere with the inGame as the name and have boolvalues with people’s names in them so if it’s true for that person, they are in the game and if it’s false, they aren’t. So something like this:

[inGame = Folder.
-Player1 = BoolValue inside of Folder.

  • Value: True

-Player2 = BoolValue inside of Folder.

  • Value: False

-Player3 = BoolValue inside of Folder.

  • Value: False]

And so on.

I will try that, I will let you know if it works!

Alright, if you need any help just let me know.

Just fyi, im in studio typing something up for you, just its taking me a bit, kids woke up :stuck_out_tongue:

2 Likes

i have sort of tried to remake what you have made, but i made it so the player has a value with there name in the folder, and if that value is there the player is in game, what would be the best way to check how many values are left in that folder, and also at the same time, keep the script from doing anything else (as this is all in a loop) until there is a last man standing

So, has what I said worked for you?

yes, but thats just rewriting the gametag system i already have, i need to workout how many are left, and the continue the loop

Ahh, alright. Glad I could help! If you need me to type it out for you I can, just might take a couple of minutes.

local status = game:GetService("ReplicatedStorage"):WaitForChild("Status")

function GetCount()
	local counter = 0
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character and v.Character:FindFirstChild("HumanoidRootPart") then
			if v.Character.HumanoidRootPart:GetAttribute("inGame") then
				counter = counter +1
			end
		end
	end
	return counter
end

while true do
	status.Value = "Intermission"
	wait(10)
	status.Value = "In Game"
	for i,v in pairs(game.Players:GetPlayers()) do
		local toTP = math.random(1,3)
		v.Character.HumanoidRootPart.CFrame = workspace.System[toTP].CFrame
		v.Character.HumanoidRootPart:SetAttribute("inGame",true)
	end
	
	while GetCount() >=  2 do
		print("in game loop")
		wait(1)
	end 
	print("there are less than 2 players in game")
end
3 Likes

Thank you @SelDraken, this has worked perfectly with no issues.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.