Round Ending Function runs thousands of times

Hey, I was fixing some bugs in my game, and I discovered a bug.

Basically, If every single player but 1 leaves the game, I have a function that runs that stops the ongoing game, And teleports the player to the lobby, Then a wait for new players commences.

But there is an issue with this, I don’t really know how to put this into words other than having a seizure. Here’s a clip.

It like teleports the player hundreds of times, Even though I have a break in the if statement that stops after it runs the end-round script.

Here is my script:

if FolderValue then
local timeValue = FolderValue:FindFirstChild("Time")

ServerStorage.PrivateEvents.CustomLoader:Fire(FoundMap:GetAttribute("CustomLoadKey"))

for i = timeValue.Value, 1, -1 do
local CheckPlayersResult = EndRound.CheckPlayers(FoundMap, SharedPlayerTable)
									
StatusValue.Value = "GAME TIME: " .. i .. "\n"

if CheckPlayersResult == true then print("Ending here 999") break end

return
end
end

ServerStorage.PrivateEvents.RoundEnding:Fire()
EndRound.EndRound(FoundMap, SharedPlayerTable)
print("Ending Here 1009")
return

All the checkplayersresult module does is check if the amount of players in-game is less than 2, So I don’t feel like I need to include it, You can ask me though.

Not a lot of context is provided here , where is this code being run? Is it a while loop? Is it a bindable event?

No, This is ran in the main game loop. This is a version that has comments. Apologies.

							if FolderValue then
								local timeValue = FolderValue:FindFirstChild("Time")
								ServerStorage.PrivateEvents.CustomLoader:Fire(FoundMap:GetAttribute("CustomLoadKey")) -- This is before the match starts.

								for i = timeValue.Value, 1, -1 do -- // This is for updating the status, And checking amount of players (every second)
									local CheckPlayersResult = EndRound.CheckPlayers(FoundMap, SharedPlayerTable)
									
									StatusValue.Value = "GAME TIME: " .. i .. "\n"

									if CheckPlayersResult == true then print("Ending here 999") break end -- // Chekcing amount of players

									task.wait(1)
									return
								end
							end

							ServerStorage.PrivateEvents.RoundEnding:Fire() -- // Table Stuff
							EndRound.EndRound(FoundMap, SharedPlayerTable) -- // Ending
							print("Ending Here 1009")

The bindable is just for table modifying, That is completely unrelated to this tho. (I replaced break with continue and it basically did the same thing just not ending)

print("Ending Here 1009")

i don’t see this print firing, one of your earlier functions must be doing it in a loop? could you post the implementation of the previous 2 functions?

wdym by the previous 2 functions? i’ll just post this entire part, and the module.

task.spawn(function()
						local FoundMap = game.Workspace:FindFirstChild(TeleportResult)

						if TeleportResult ~= false and FoundMap then
							local FolderValue = FoundMap:WaitForChild("Data")

							ServerStorage.PrivateEvents.ForceEndRound.Event:Connect(function(DB)
								if DB == true then
									ServerStorage.PrivateEvents.RoundEnding:Fire()
									EndRound.EndRound(FoundMap, SharedPlayerTable)
									print("Ending Here")
								end
							end)

							if FolderValue then
								local timeValue = FolderValue:FindFirstChild("Time")
								ServerStorage.PrivateEvents.CustomLoader:Fire(FoundMap:GetAttribute("CustomLoadKey")) -- This is before the match starts.

								for i = timeValue.Value, 1, -1 do -- // This is for updating the status, And checking amount of players (every second)
									local CheckPlayersResult = EndRound.CheckPlayers(FoundMap, SharedPlayerTable)
									
									StatusValue.Value = "GAME TIME: " .. i .. "\n"

									if CheckPlayersResult == true then print("Ending here 999") break end -- // Chekcing amount of players

									task.wait(1)
									return
								end
							end

							ServerStorage.PrivateEvents.RoundEnding:Fire() -- // Table Stuff
							EndRound.EndRound(FoundMap, SharedPlayerTable) -- // Ending
							print("Ending Here 1009")
							return
						else
							return
						end
					end)

Module:

function Round.CheckPlayers(map, tbl : any)
	if #tbl <= SettingsMod.RequiredToEndRound then
			return true
	else
		return false
	end
end

ServerStorage.PrivateEvents.RoundEnding:Fire() -- // Table Stuff
EndRound.EndRound(FoundMap, SharedPlayerTable) -- // Ending

Where are these 2 functions defined? What do they do? This is the 2 functions i was asking for

My bad. This is endround.endround:

function Round.EndRound(ClonedMap : Model, PlayerTable)
	if ClonedMap then
		local s, e = pcall(function()
			for index, ingamePlayers in ipairs(game:GetService("Players"):GetPlayers()) do
				ingamePlayers:LoadCharacter()
			end
			
			if ClonedMap then
				ClonedMap:Destroy()
				ReplicatedStorage:WaitForChild("Events").Map_Events.Lighting:FireAllClients(2, nil); 
				table.clear(SharedPlayerTable)
				SharedPlayerTable = {}
				SettingsMod.EndRound = false
			end
		end)
		
		if s then
			return true
		else
			warn("<<< SERVER INFO >>> "..e)
			return false
		end
	else
		print("? Why isn't this existing? What?")
	end
end

And this is privateevents

BindableRoundEnding.Event:Connect(function()
	if SharedPointsTable ~= {} then
		print("Ending")
		task.wait(1)
		EndRoundGUI:FireAllClients(SharedPointsTable)
		table.clear(SharedPointsTable); SharedPointsTable = {}
	end
end)

There doesn’t seem to be anything wrong with this, from what im seeing its caling endround multiple times? could you debug a little bit more, follow the call stack to the end, and see what it calls again, add a print at the beginning of Round.EndRound to see if it gets called multiple times, etc…

sorry im really dumb can you like rephrase that? if im right, what you’re saying to me is to print debug perhaps? earlier i tried a method, and it was printing Ending here 999 a bunch in the output. but im really confused on what you mean sorry

yeah add prints to your code, also show me the function for EndRoundGUI Remote Event, and the function for Map_Events Lightning event

thoses are completely irrelevant to this, all they do is make a ui pop up. but i will print debug.

(post print debugging)

this is the output, it doesn’t at all re spam anything. but still teleports the player a bunch.
image

So if it doesn’t print inside the EndRound function multiple times, it shouldn’t be resetting your character multiple times, you could try setting the players cframe instead of resetting the player, instead of doing

			for index, ingamePlayers in ipairs(game:GetService("Players"):GetPlayers()) do
				ingamePlayers:LoadCharacter()
			end

you can try

			for index, ingamePlayers in ipairs(game:GetService("Players"):GetPlayers()) do
				if not ingamePlayers.Character then continue end

				ingamePlayers.Character.PrimaryPart.CFrame = --[[ reference to spawnpoint ]].CFrame
			end
1 Like

Also check your code, maybe you have resetting somewhere else too doing it in a loop

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