Lobby respawn script running numerous times

Hello! Basically, I was testing a lobby system, When I came across an error, Basically, It runs the “EndRound” Function every single second, Instead of just ending the script and restarting a new match.

Here is a visual:

And here is a snippet of the code:

local function EndRound(clonedMap)
	for _, player in ipairs(playerTable) do
		if player.Character then
			MapUnloadEvent:FireClient(player)
			player:LoadCharacter()
			RoundEndingEvent:Invoke()
		end
	end
	clonedMap:Destroy()
	print("Sucessfully Ended Round!")
end

local function CheckAmountOfPlayers(ClonedMap)
	task.wait(1)
	if #workspace.Ingame:GetChildren() <= 1 then
		EndRound(ClonedMap)
		print("End")
	end
end

local valuesFolder = clonedMap:WaitForChild("Data")
				
				if valuesFolder then
					local timeValue = valuesFolder:FindFirstChild("Time")
					if timeValue then
						for i = timeValue.Value, 1, -1 do
							StatusValue.Value = "GAME TIME: " .. i
							CheckAmountOfPlayers(clonedMap)
							task.wait(1)
						end
					end
				end
				
				EndRound(clonedMap)

Help appreciated :doh:

1 Like

You seem to be invoking this every player?
Not sure what it links to but could be the reason

How would I go about fixing this?

Move the invoke outside of the for loop

local function EndRound(clonedMap)
	for _, player in ipairs(playerTable) do
		if player.Character then
			MapUnloadEvent:FireClient(player)
			player:LoadCharacter()
		end
	end
	RoundEndingEvent:Invoke()
	clonedMap:Destroy()
	print("Sucessfully Ended Round!")
end

this may fix it

Same issue occurs, It just loops for every second left on the timer.

Have you made sure that the workspace.Ingame folder has more than 1 object inside?

1 Like

image
image
Yea