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.
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
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