Script timeout exhausted allowed execution time

Hello, i made a system of pickup and the script crashes with the bug ‘script timeout exhausted allowed execution time’

Here’s the script:

for _, Pickup in pairs(Map:FindFirstChild("Pickup"):GetChildren()) do
			local TouchedPlayers = {}
			Pickup.Touched:Connect(function(Hit)
				if Hit.Parent:FindFirstChildOfClass("Humanoid") then
					local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
					if Player then
						if Player.Team == game.Teams.Lobby then return end

						if table.find(TouchedPlayers, Player) then return end
						table.insert(TouchedPlayers, Player)

						-- script
					end
				end
			end)
			wait(0.1)
		end

This is caused when the script takes an exceedingly long amount of time to load. It normally happens when you have a loop with no wait and no soon ending

But i am not wrong there’s a wait(0.1)

Nothing in the snippet you posted seems like it’d make the script loop forever
Is that the entire script?

Yeah, I’d guess the issue is within another part of the script

If i add a game:GetService("RunService").Stepped:Wait() to every loops of the code, it should be fixed?

Wouldn’t have thought so, but you could try it?
Or alternatively, I suppose you could hard code it - depending on how long that would take

Is this your entire script? I don’t think this part of the script has the error.

1 Like

It might but for future reference things like:

while true do
       ...
end

will always cause the error

Yes i experienced that issue once, which crashed my computer :sweat_smile:

What does ‘hard code it’ means

Hardcoding is when you already have values set instead of figuring them out, for instance instead of doing value = 3 + 7 * 4 you would do value = 31 you can’t always do this though

In this case, instead of looping through the pickups and making the touched event, just write out each individual event, e.g.

pickup1.Touched:Connect(function()
   -- code
end)

pickup2.Touched:Connect(function()
    -- code
end)

I can’t, each map have a different amount of pickups

no idea, but I indented it better, I think. And see the comment in the code.

Um, also, you don’t have a debounce in your touched event. Touched has never worked correctly for me, without a debounce…

You might need to put a debounce in the touch function

When I get that error I normally just use wait(0.00000000000000000000000000000000000000000001)
It always works for some reason

The quickest that a yield will come back to a thread is wait() =~= wait(.03) =~= 1 Frame

1 Like

Yeah no Wait() usualy crashes. You are probably making a ton of Connects, which never get resolved.
put a Print at every-other line, and you will def find your error.

This is blatantly untrue. The break keyword exists for a reason.
@MagicalAnteater , include the following to make sure your script doesn’t timeout:

local clock = os.clock()
for _, Pickup in pairs(Map:FindFirstChild("Pickup"):GetChildren()) do
	local TouchedPlayers = {}
	if os.clock() - clock => 0.1 then
		wait(.1)
		clock = os.clock()
	end
	Pickup.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChildOfClass("Humanoid") then
			local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
			if Player and (Player.Team == game.Teams.Lobby or table.find(TouchedPlayers, Player)) then
				table.insert(TouchedPlayers, Player)
				-- script
			end
		end
	end)		
end