Exhausted Execution Time

I’m struggling to understand why I’m getting exhausted allowed execution time on a certain part of my code; all of the loops wait() and when I click on the error, it directs me to the wait(10) line of the following script:

function Round()
	local RoundCount = tostring(PlayerCount.Value)
	local Events = true
	
	for _,house in pairs(workspace:FindFirstChild("Houses"):GetChildren()) do
		if house:FindFirstChild("HouseOwner") then
			local plr = game.Players:FindFirstChild(house.HouseOwner.Value)
			local char = plr.Character
			local hum = char:FindFirstChild("Humanoid")

			if plr and hum then
				hum.Died:Connect(function()
					house.HouseOwner:Destroy()
					plr.Team = SpectatorsTeam
				end)
			end
		end
	end
	
	while EnoughPlayers do
		TextEvent:FireAllClients("Game Started")
		wait(1)
		TextEvent:FireAllClients("")
		wait(5)
		
		while Events do
			if EnoughPlayers then
				local randomevent = coroutine.create(function() -- creates a sub-event to allow code to run simultaneously
					events.RandomEvent()
				end)
				coroutine.resume(randomevent)
				
				wait(10)
			else
				Events = false
				break
			end
		end
		
		wait()
		break
	end
	
	if not EnoughPlayers then
		RespawnPlayers()
		Waiting()
	end
end

edit: It has only started happening since I added the death event, and it happens on respawn

Try adding the wait(10) inside the coroutine

That means the code is going to run again without waiting, meaning I have an infinite amount of random events.

1 Like

Yeah, it looks like you need to add a wait(some time) somewhere.
You should try to put wait in random places, and see what you come up with. I don’t see a problem with this code.

1 Like

I think you forgot a wait in your events loop.

while Events do
wait()
			if EnoughPlayers then
				local randomevent = coroutine.create(function() -- creates a sub-event to allow code to run simultaneously
					events.RandomEvent()
				end)
				coroutine.resume(randomevent)
				
				wait(10)
			else
				Events = false
				break
			end
		end
1 Like

It has been resolved, the issue was in the module script as when the player died, the module script was looping through players that didn’t exist without wait(). In regards to @Nightmare4into96, I don’t know how I missed that, thanks.

1 Like