Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 8 years ago

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!

01print("Advanced SpecialDoor Script loaded")
02 
03 
04 
05permission = { "Fire Dorm" }
06Door = script.Parent.Parent.PersonDoor
07Door2 = script.Parent.Parent.PersonDoor2
08DoorMesh = script.Parent.Parent.PersonDoor.Mesh
09Sensor = script.Parent
10Sounds = script.Parent.Parent.Sounds
11 
12function checkOkToLetIn(name)
13    for i = 1,#permission do
14 
15        if (string.upper(name) == string.upper(permission[i])) then return true end
View all 87 lines...

Thanks!

1 answer

Log in to vote
2
Answered by
xAtom_ik 574 Moderation Voter
8 years ago

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

01local buttonPressed = false
02--Store whether the button is pressed in a local variable
03 
04Workspace.Button.Touched:connect(function(hit)
05    if not buttonPressed then
06    -- Is it not pressed?
07 
08        buttonPressed = true
09        -- Mark it as pressed, so that other handlers don't execute
10 
11        print("Button pressed")
12        wait(1)
13        print("Hi :D")
14        -- Do Stuff
15 
16        buttonPressed = false
17        -- Mark it as not pressed, so other handlers can execute again
18    end
19end)

Taken from the wiki


Fixed script

Here is your fixed script!

01print("Advanced SpecialDoor Script loaded")
02 
03 
04 
05permission = { "Fire Dorm" }
06Door = script.Parent.Parent.PersonDoor
07Door2 = script.Parent.Parent.PersonDoor2
08DoorMesh = script.Parent.Parent.PersonDoor.Mesh
09Sensor = script.Parent
10Sounds = script.Parent.Parent.Sounds
11debounce = false
12 
13function checkOkToLetIn(name)
14    for i = 1,#permission do
15 
View all 93 lines...

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.

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. callmehbob 54 — 8y
Ad

Answer this question