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

Gate script question, how do I put a debounce in it?

Asked by 10 years ago

What I want to do is this: Put a debounce in the script. SO then if you click the button the gate will slide thorough. But if you click it again it will slide back. But you have to wait a couple seconds for it to slide back. I am also toying around with it to see if I can make it not make the gate go too far if it is repetedly pressed. I would love it if someone would help me!

DoorSystem = script.Parent 
Door = DoorSystem.Door1 
DoorPad = DoorSystem.Open.ClickDetector
local padPressed = false

function Slide()
    if not padPressed then
        padPressed = true
        for i = 1, 500 do
            Door.CFrame = Door.CFrame + Vector3.new(0, 0, 0.1)
            wait()
        end
    end
end

DoorPad.MouseClick:connect(Slide)
0
(I responded to your comment; I had a typo in my script which made it move extraordinarily faster than it is meant to) BlueTaslem 18071 — 10y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

This isn't quite a standard debounce.

Basically, we have to make new button presses interrupt old ones. Instead of doing the loop in the event, though, it really is easier to make the event just write the direction, rather than actually repeatedly act on it:

DoorSystem = script.Parent 
Door = DoorSystem.Door1 
DoorPad = DoorSystem.Open.ClickDetector

home = Door.CFrame -- Where the door STARTS
open = 0 -- How far (in studs) the door is open

direction = 0
speed = 0.3 -- studs/second

function Slide()
    -- Flip direction (turn around)
    if direction <= 0 then
        direction = 1
    else
        direction = -1
    end
end

DoorPad.MouseClick:connect(Slide)


while true do 
    open = open + direction * speed * wait()
    open = math.max(0, open) -- Not a negative opening
    open = math.min(50, open) -- At most 50 studs open
    Door.CFrame = home + Vector3.new(0, 0, open)
end

This won't close on its own, though.

A partial solution is this:

function Slide()
    -- Flip direction (turn around)
    if direction <= 0 then
        direction = 1
        wait(5)
        direction = -1
    else
        direction = -1
    end
end

The problem is input like this:

  • 0s : Click (starts opening)
  • 3s : Click (starts closing)
  • 4s : Click (starts opening again)
  • 5s : wait(5) from first click just fired, so door closes

That interruption in the last moment is the problem. Basically, we need to measure if there has been another click in the last 5 seconds:

lastClick = 0

function Slide()
    -- Flip direction (turn around)
    lastClick = tick()
    now = tick()
    if direction <= 0 then
        direction = 1
        wait(5)
        if lastClick <= now then
            direction = -1
        end
    else
        direction = -1
    end
end
0
I dont know how to post the script together. I'm toying around with it and trying. But I dont know what code sippet goes in what order. BosswalrusTheCoder 88 — 10y
0
The first, full snippet, but replace the definition of `Slide()` in it with the last definition (the last snippet) BlueTaslem 18071 — 10y
0
Is there any way to make it go slower? BosswalrusTheCoder 88 — 10y
0
My apologies, I had a typo. The `/ wait()` should have been `* wait()`. After fixing that, the `speed` variable will refer to the correct quantity. BlueTaslem 18071 — 10y
Ad

Answer this question