Anyway to stop a script if its re-run again?

I made a door that when it detects when a certain keycard is touching it, it will fire to the door control and open the door with cool animation on the keycard scanner screen, and after a while once debounce is turned back it will go back to a screensaver, however, when someone uses the door multiple times the if-then keeps running and ends up flashing between the screensaver and opening door decals or activating the screensaver early.
Is there any way to stop the script if it’s been run again or any way to detect If the script was run again?
here’s an example script

hitbox.Touched:Connect(function(idk)
	if idk.Parent.Name == "Keycard" and debounce then
		debounce = false
		-- do door things and whatever
        wait(12)
		debounce = true
        wait(25)
		-- screensaver goes on 
        --(would only want it to run if nobody used the key scanner for awhile)
	end
end)

You could just use the .Disabled proprety into a script,localscript and others. Use this code:

hitbox.Touched:Connect(function(idk)
	if idk.Parent.Name == "Keycard" and debounce then
		debounce = false
		-- do door things and whatever
        wait(12)
		debounce = true
        wait(25)
		-- screensaver goes on 
       script.Parent.Script.Disabled = true
	end
end)

Is there any way to detect if the script has been re run again and then disable it? also if I did do that when the script is run again wouldn’t it disable the currently running script if someone was trying to open the door again?

1 Like

Try this

local check = 1
hitbox.Touched:Connect(function(idk)
if check == 2 then 
script.Disabled = true
else
	if idk.Parent.Name == "Keycard" and debounce then
		debounce = false
		-- do door things and whatever
        wait(12)
		debounce = true
        wait(25)
		-- screensaver goes on 
        check = check + 1

              end
       end
end)