Script timeout: exhausted allowed execution time

Hello, when I try to run my script

local Target = game.ReplicatedStorage.NPC
local Script = game.ReplicatedStorage.NPC:WaitForChild("Script")

function onTouched(hit)
	if game.Workspace.Value.Value == 1 then
		Target.Parent = game.Workspace
		Script.Disabled = false
		wait(0.50)
		Target.Parent = game.ReplicatedStorage
		Script.Disabled = true
		script.Disabled = true
		wait()
	end
end
script.Parent.Touched:connect(onTouched)

it freezes my testing window and says “Script timeout: exhausted allowed execution time” in the output, eventually it unfreezes 20 seconds later. Why is this happening and is there a simple way of fixing this?

1 Like

Add a debounce. When you touch the brick, it’s firing it many times, not just once. If you need help with that I can help. It is very simple though.

1 Like

Hmmm, try this

local Target = game.ReplicatedStorage:WaitForChild("NPC")
local Script = Target:WaitForChild("Script")
function onTouched(hit)
	if game.Workspace.Value.Value == 1 then
        Target.Parent = game.Workspace
	     Script.Disabled = false
         wait(0.50)
         workspace:WaitForChild("NPC").Parent = game.ReplicatedStorage
          workspace:WaitForChild("NPC"):WaitForChild("Script").Disabled = true
      end
end
end
script.Parent.Touched:connect(onTouched)

@JigglyWiggles @static2240 Still not working, now It’s not giving me the error message for both your guy’s solutions but It’s not doing anything anymore.

debounce should be added because you need your NPC.Script to be enabled and disabled only once. This should work

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Target = ReplicatedStorage.NPC
local Script = ReplicatedStorage.NPC.Script

local debounce = false
function onTouched(hit)
	if not debounce and workspace.Value.Value == 1 then
		debounce = true
		print("Touched only once")
		
		Target.Parent = workspace
		Script.Disabled = false
		wait(0.50)
		Target.Parent = ReplicatedStorage
		Script.Disabled = true
		script.Disabled = true
	end
end
script.Parent.Touched:connect(onTouched)

This does work. But it brings back up the original “Script timeout: exhausted allowed execution time” problem that I was facing before.image

What exactly are you trying to do? It looks like you want to spawn in an NPC character than then despawn it when a brick is touched. Is this true?

local debounce = false

function onTouched(hit)
	if game.Workspace.Value.Value == 1 then
if debounce == false then
debounce = true
		Target.Parent = game.Workspace
		Script.Disabled = false
		wait(0.50)
		Target.Parent = game.ReplicatedStorage
		Script.Disabled = true
		script.Disabled = true
		wait()
delay(1.3, function()
debounce = false
end)
	end
end
end
script.Parent.Touched:Connect(onTouched)

Try this.

Yes. And also enable a script.

What about the script you are trying to enable is there a loop inside that script ?
Roblox Lua scripts are extremely fast so fast that if you have a loop say:

 while true do
      print("This thing")
 end

It will freeze studio for 10-20 seconds and you will see in the output that “This thing” was printed around 15 - 20 thousand times and you will have your error so your issue is most likely in the script your trying to enable
(Note: When working with loops always add a “wait(Amount of seconds”)

1 Like

Perhaps, I will check. I’ll get back soon.