My game wont end when there's only one player left

I honestly dont know whats wrong

local function teleportToLobby()
	roundRunning = false
	for i, character in pairs(playersInGame:GetChildren()) do
		changeStatus("Teleporting players to lobby...")
		local selectedSpawn = lobbySpawns[math.random(1, #lobbySpawns)]
		character.HumanoidRootPart.CFrame = selectedSpawn.CFrame
		character.Parent = playersInLobby
	end
	
	gameFolder:WaitForChild("Map"):ClearAllChildren()
	
end

local function teleportToGame()
	if not map:FindFirstChild("Map") and not gameFolder:WaitForChild("Map"):FindFirstChild("Sharknado") then
		local map = gameFolder:WaitForChild("Map")
		local newMap = ClonedMap:Clone()
		newMap.Parent = map
	else
		return
	end
	
	
	wait(1.75)
	local gameSharknado = game.ReplicatedStorage.GameObjects.Sharknado:Clone()
	gameSharknado.Parent = map
	roundRunning = true
	
	for i, character in pairs(playersInLobby:GetChildren()) do
		changeStatus("Teleporting players to game...")
		local randomSpawns = gameFolder.Map.Map.Spawns:GetChildren()
		local selectedSpawn = randomSpawns[math.random(1, #randomSpawns)]
		character.HumanoidRootPart.CFrame = selectedSpawn.CFrame
		character.Parent = playersInGame
	end
	
	
	
end

while true do
	
	wait(intermissionTime)
	
	teleportToGame()
	
	while true do
		wait()
		local amtOfPlayers = playersInGame:GetChildren()
		if #amtOfPlayers <= 1 then
			roundRunning = false
			for i, char in pairs(playersInGame:GetChildren()) do
				if char then
					changeStatus(char.Name.." won!")
				end
			end
			teleportToLobby()
			break
		else
			return
		end
	end
	
end

I don’t see the point of the return in the else statement as there’s no function covering the code, and might result in an error?

Instead of a while loop you could store all the players in a folder (or a clone of them) and using a ChildRemoved():Connect(function()) if his number is <=1 then the match ends else the game continues

Yes, that worked. Thank you for your help!

1 Like

He should be using the break/continue statements instead as those are purposed for loops/iterations, the execution of a break statement will break out of the loop entirely and the execution of a continue statement will skip the current iteration cycle and move to the direct next iteration cycle of the loop.

Considering their use of a return statement I presume they should be using a break statement instead.