Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Where to put the "wait" in this script to make a cooldown?

Hi there! I have a wonderful door I love so dear, but I cannot figure out to put a cooldown on its usage. It is a door that allows a tool (a card) to grant access to the door (tool turns the door to non-collidable).

Here's the problem - when a card hits the sensor of the door to allow access, if you move your character back and forth, it plays the "opening" and other noises a million times. It needs a cooldown of some sort so the it only registers the card once, say, every 10 seconds. My friend and I believe it should need just a simple "wait" command, we have been messing with it but can't quite figure this one out! Messing with the current wait commands just delays the door with doing what it needs to do, not putting a cooldown on the card registering to the door. I would very much appreciate the help!

print("Advanced SpecialDoor Script loaded")



permission = { "Fire Dorm" }
Door = script.Parent.Parent.PersonDoor
Door2 = script.Parent.Parent.PersonDoor2
DoorMesh = script.Parent.Parent.PersonDoor.Mesh
Sensor = script.Parent
Sounds = script.Parent.Parent.Sounds

function checkOkToLetIn(name)
	for i = 1,#permission do
		
		if (string.upper(name) == string.upper(permission[i])) then return true end
	end
	return false
end

function onTouched(hit)
		print("Sensor Hit")
		local human = hit.Parent:findFirstChild("Humanoid")
		if (human ~= nil ) then
			-- 
			print("Human touched door")
			
			if (checkOkToLetIn(human.Parent.Name)) then
				print("Human passed test")
				Wait(0.001)
                                script.Parent.Parent.Dot1.Transparency = 0
                                Sounds.Beep:Play()
                                wait(0.4)
                                script.Parent.Parent.Dot2.Transparency = 0
                                Sounds.Beep2:Play()
				wait(0.4) 
                                script.Parent.Parent.Dot3.Transparency = 0
                               	Sounds.Beep2:Play()
				wait(0.4) 
                                script.Parent.Parent.Open.Transparency = 0
                                script.Parent.Parent.Close.Transparency = 1
                               	Sounds.Beep2:Play()
                                wait(0.2)
                              	Sounds.OpenSound1:Play()
                                wait(0.1)
                                script.Parent.Parent.HandlePart3.Transparency = 0
                                script.Parent.Parent.HandlePart4.Transparency = 0
                                script.Parent.Parent.HandlePart.Transparency = 1
                                script.Parent.Parent.HandlePart2.Transparency = 1
								script.Parent.Parent.Handle2.Transparency = 1
								script.Parent.Parent.Handle5.Transparency = 0
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.9, 0.5, 1.01)
                                Door.Transparency = 1
								Door2.Transparency = 0
								Door2.Wedge.Transparency = 0
								Door2.Wedge2.Transparency = 0
								Door.Wedge.Transparency = 1
								Door.Wedge2.Transparency = 1
                                wait(0.02)
				Door.CanCollide = false
                                script.Parent.Parent.Dot1.Transparency = 1
                                script.Parent.Parent.Dot2.Transparency = 1
                                script.Parent.Parent.Dot3.Transparency = 1
				wait(3) 

                                script.Parent.Parent.Open.Transparency = 1
                                script.Parent.Parent.Close.Transparency = 0
                                script.Parent.Parent.HandlePart3.Transparency = 1
                                script.Parent.Parent.HandlePart4.Transparency = 1
                                script.Parent.Parent.HandlePart.Transparency = 0
                                script.Parent.Parent.HandlePart2.Transparency = 0
								script.Parent.Parent.Handle2.Transparency = 0
								script.Parent.Parent.Handle5.Transparency = 1
								Sounds.OpenSound2:Play()
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.01, 0.01, 0.01)
				Door.CanCollide = true
								Door.Transparency = 0 
								Door2.Transparency = 1
								Door2.Wedge.Transparency = 1
								Door2.Wedge2.Transparency = 1
								Door.Wedge.Transparency = 0
								Door.Wedge2.Transparency = 0
			end
		end

end

connection = Sensor.Touched:connect(onTouched) 

Thanks!

1 answer

Debounce

I understand why you have tried to be using the wait(10) for the script, but a debounce system is quite effective in my opinion. Basically, you set a variable in your script for example, named 'debounce'. Then before you do anything in the function, you check if debounce is false, and if it is, then set it to true and continue. At the end, you need to then add the wait(10) and then set the variable to false.


Example

local buttonPressed = false
--Store whether the button is pressed in a local variable
 
Workspace.Button.Touched:connect(function(hit)
    if not buttonPressed then
    -- Is it not pressed?
 
        buttonPressed = true
        -- Mark it as pressed, so that other handlers don't execute
 
        print("Button pressed")
        wait(1)
        print("Hi :D")
        -- Do Stuff
 
        buttonPressed = false
        -- Mark it as not pressed, so other handlers can execute again
    end
end)

Taken from the wiki


Fixed script

Here is your fixed script!

print("Advanced SpecialDoor Script loaded")



permission = { "Fire Dorm" }
Door = script.Parent.Parent.PersonDoor
Door2 = script.Parent.Parent.PersonDoor2
DoorMesh = script.Parent.Parent.PersonDoor.Mesh
Sensor = script.Parent
Sounds = script.Parent.Parent.Sounds
debounce = false

function checkOkToLetIn(name)
    for i = 1,#permission do

        if (string.upper(name) == string.upper(permission[i])) then return true end
    end
    return false
end

function onTouched(hit)
	if debounce == false then
		debounce = true
        print("Sensor Hit")
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil ) then
            -- 
            print("Human touched door")

            if (checkOkToLetIn(human.Parent.Name)) then
                print("Human passed test")
                Wait(0.001)
                                script.Parent.Parent.Dot1.Transparency = 0
                                Sounds.Beep:Play()
                                wait(0.4)
                                script.Parent.Parent.Dot2.Transparency = 0
                                Sounds.Beep2:Play()
                wait(0.4) 
                                script.Parent.Parent.Dot3.Transparency = 0
                                Sounds.Beep2:Play()
                wait(0.4) 
                                script.Parent.Parent.Open.Transparency = 0
                                script.Parent.Parent.Close.Transparency = 1
                                Sounds.Beep2:Play()
                                wait(0.2)
                                Sounds.OpenSound1:Play()
                                wait(0.1)
                                script.Parent.Parent.HandlePart3.Transparency = 0
                                script.Parent.Parent.HandlePart4.Transparency = 0
                                script.Parent.Parent.HandlePart.Transparency = 1
                                script.Parent.Parent.HandlePart2.Transparency = 1
                                script.Parent.Parent.Handle2.Transparency = 1
                                script.Parent.Parent.Handle5.Transparency = 0
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.9, 0.5, 1.01)
                                Door.Transparency = 1
                                Door2.Transparency = 0
                                Door2.Wedge.Transparency = 0
                                Door2.Wedge2.Transparency = 0
                                Door.Wedge.Transparency = 1
                                Door.Wedge2.Transparency = 1
                                wait(0.02)
                Door.CanCollide = false
                                script.Parent.Parent.Dot1.Transparency = 1
                                script.Parent.Parent.Dot2.Transparency = 1
                                script.Parent.Parent.Dot3.Transparency = 1
                wait(3) 

                                script.Parent.Parent.Open.Transparency = 1
                                script.Parent.Parent.Close.Transparency = 0
                                script.Parent.Parent.HandlePart3.Transparency = 1
                                script.Parent.Parent.HandlePart4.Transparency = 1
                                script.Parent.Parent.HandlePart.Transparency = 0
                                script.Parent.Parent.HandlePart2.Transparency = 0
                                script.Parent.Parent.Handle2.Transparency = 0
                                script.Parent.Parent.Handle5.Transparency = 1
                                Sounds.OpenSound2:Play()
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.01, 0.01, 0.01)
                Door.CanCollide = true
                                Door.Transparency = 0 
                                Door2.Transparency = 1
                                Door2.Wedge.Transparency = 1
                                Door2.Wedge2.Transparency = 1
                                Door.Wedge.Transparency = 0
                                Door.Wedge2.Transparency = 0
								wait(10)
								debounce = false
            end
        end
	end

end

connection = Sensor.Touched:connect(onTouched) 

Thanks for reading

Thanks for reading my answer! If it help you, make sure to upvote it or set it as the correct answer! Thanks! Hope it helped.

1 comment

0

Thank you so much for your answer and explaining the debounce to me! The debounce is something else my friend tried but unfortunately your script breaks the door :( . Not entirely, though - it only sometimes decides to work. It is quite strange! Thank you for trying to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.