Script Not Working: script timeout exhausted allowed execution time

local AlarmValue = false
local Magnet = script.Parent.Parent.Magnet
local Main = script.Parent

local AlarmActiveValue = game.Workspace["Tan's Technology Dynanc Security Alarm"].Values.AlarmActive

while true do
	local Distance = (Main.Position - Magnet.Position).Magnitude
	
	if Distance >= 10 then
		AlarmValue = true
		
		if AlarmValue == true then
			AlarmActiveValue = true
		
	else if Distance <= 10 then
			AlarmValue = false
				if AlarmValue == false then
					AlarmActiveValue = false
				end
			end	
		end
	end
end

This code is operated to keep checking if a part is 10 studs away from another part to make a door ocntact sensor work. However, on line 7 (“While True Do”) it keeps not fucntioning.

The error ouput says “Script Timeout: Exhausted Allowed Execution Time”

I am not sure on how to fix it and wondering if anyone could hoepfully help me within fixign this error that is killing my brain.

You need to add some sort of cooldown to the loop, so that it doesn’t try to run a bajillion times in a single frame.

Just add task.wait() to either the very start or very end of the script

Do what @PoppyandNeivaarecute said by not calling “while true do” just call “while wait() do”. This will repeat the upfollowing process but with little debounce so that it doesn’t exceed the processing limit.

Like This?

local AlarmValue = false
local Magnet = script.Parent.Parent.Magnet
local Main = script.Parent

local AlarmActiveValue = game.Workspace["Tan's Technology Dynanc Security Alarm"].Values.AlarmActive

while true do
	local Distance = (Main.Position - Magnet.Position).Magnitude
	
	if Distance >= 10 then
		AlarmValue = true
		
		if AlarmValue == true then
			AlarmActiveValue = true
		
	else if Distance <= 10 then
			AlarmValue = false
				if AlarmValue == false then
					AlarmActiveValue = false
				end
			end	
		end
	end
end

task.wait()

The task.wait() needs to be inside the loop, so like this:

local AlarmValue = false
local Magnet = script.Parent.Parent.Magnet
local Main = script.Parent

local AlarmActiveValue = game.Workspace["Tan's Technology Dynanc Security Alarm"].Values.AlarmActive

while true do
	local Distance = (Main.Position - Magnet.Position).Magnitude
	
	if Distance >= 10 then
		AlarmValue = true
		
		if AlarmValue == true then
			AlarmActiveValue = true
		
	else if Distance <= 10 then
			AlarmValue = false
				if AlarmValue == false then
					AlarmActiveValue = false
				end
			end	
		end
	end
    task.wait()
end

Ahh ok ill test it in a second and let you know how i get on

Hey! I appreciate you helping me within this script.

I wish to let you know that this solution has worked.

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