Round not Ending when there's one or no players

Hey Developers!

So I’ve been working on this FFA-Round Based game with different maps and a lobby that takes inspiration from the 2006 - 2008 era of Roblox. I’m pretty close to being done with the game, except the biggest problem which is the round system not working.

What I’m trying to accomplish is to have the round system end when there’s one player left or no players left.

I’ve tried making new functions to count all the players on the map and detect when one is left or none to end the round, but when the players load into the map, the round ends immediately!

After that, I’ve been lost on what to do. The code below is my round system.

RoundSystemHandler:

local Lobby = game.Workspace.Lobby
local Maps = game.ReplicatedStorage.Maps:GetChildren()
local Status = game.ReplicatedStorage.Status

local function getPlayersInMap(map)
    local count = 0
    for _, player in pairs(game.Players:GetPlayers()) do
        local char = player.Character
        if char and char:IsDescendantOf(map) then
            count = count + 1
        end
    end
    return count
end

-- Game Loop
while true do
    -- Intermission
    for i = 25, 0, -1 do
        Status.Value = "Intermission: ".. i
        task.wait(1)
    end

    -- Map Selector
    local ChosenMap = Maps[math.random(1 , #Maps)]
    local CloneMap = ChosenMap:Clone()
    CloneMap.Parent = game.Workspace
    Status.Value = "Map: ".. CloneMap.Name

    task.wait(3)

    -- Teleport players to map
    for _, Player in pairs(game.Players:GetPlayers()) do
        local character = Player.Character
        if character then
            local humanoidrootpart = character.HumanoidRootPart
            humanoidrootpart.CFrame = CloneMap.TeleportationPoint.CFrame
        end
    end

    -- Round Time
    for i = 95, 0, -1 do
        Status.Value = "Round: ".. i

        -- Check player count in the map
        local playerCount = getPlayersInMap(CloneMap)
        if playerCount <= 1 then
            break  -- End the round early if 1 or 0 players left
        end

        task.wait(1)
    end

    -- Teleport players to Lobby
    for _, Player in pairs(game.Players:GetPlayers()) do
        local char = Player.Character
        if char then
            local humanoidrootpart = char.HumanoidRootPart
            humanoidrootpart.CFrame = Lobby.TeleportationPoint.CFrame
        end
    end

    -- Destroy Maps
    CloneMap:Destroy()
end

If anyone can help me solve this issue, please let me know!

1 Like

When you move a character, it still has the same parent (which is workspace), so your check will not work. I would use attributes and check if any characters still have an attribute that we set when the round starts.

Code:

local Lobby = game.Workspace.Lobby
local Maps = game.ReplicatedStorage.Maps:GetChildren()
local Status = game.ReplicatedStorage.Status

local function playersInMap()
	local count = 0
	
	for _, player in pairs(game.Players:GetPlayers()) do
		local char = player.Character
		if char and char:GetAttribute("InGame") then
			count += 1
			
			if count > 1 then
				return true
			end
		end
	end
	
	return false
end

-- Game Loop
while true do
	-- Intermission
	for i = 25, 0, -1 do
		Status.Value = "Intermission: ".. i
		task.wait(1)
	end

	-- Map Selector
	local ChosenMap = Maps[math.random(1 , #Maps)]
	local CloneMap = ChosenMap:Clone()
	CloneMap.Parent = workspace
	Status.Value = "Map: ".. CloneMap.Name

	task.wait(3)

	-- Teleport players to map
	for _, Player in pairs(game.Players:GetPlayers()) do
		local character = Player.Character
		if character then
			character:SetAttribute("InGame", true)
			
			local humanoidrootpart = character.HumanoidRootPart
			humanoidrootpart.CFrame = CloneMap.TeleportationPoint.CFrame
		end
	end

	-- Round Time
	for i = 95, 0, -1 do
		Status.Value = "Round: ".. i

		-- Check player count in the map
		if not playersInMap() then
			break  -- End the round early if 1 or 0 players left
		end

		task.wait(1)
	end

	-- Teleport players to Lobby
	for _, Player in pairs(game.Players:GetPlayers()) do
		local char = Player.Character
		if char then
			char:SetAttribute("InGame", nil)
			
			local humanoidrootpart = char.HumanoidRootPart
			humanoidrootpart.CFrame = Lobby.TeleportationPoint.CFrame
		end
	end

	-- Destroy Maps
	CloneMap:Destroy()
end
2 Likes

Thank you so much! I really appreciate the help!

1 Like

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