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

How do I fix the debounce?

Asked by 6 years ago
Edited 6 years ago
wait(1)
local debounce = false


function Own(hit)
    if debounce == false then
        local player = hit.Parent:FindFirstChild("Humanoid")
        if player then
            local owner = Instance.new('StringValue', script.Parent)
            local debounce = true
            wait(2)
            local debounce = false
        end
    end
end



script.Parent.Head.Touched:connect(Own)
0
We need to know what is wrong with your script in order to help. lukeb50 631 — 6y
0
Please edit your post and add the console output cailir 284 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Nevermind, I fixed it, this is the working script.

The problem with the first one was, after it was touched, alot of String Values would show up instead of 1.

wait(1)
local debounce = false


function Own(hit)
    if not debounce then
        debounce = true
        local player = hit.Parent:FindFirstChild("Humanoid")
        if player then
            local owner = Instance.new('StringValue', script.Parent)
            wait(2)
            local debounce = false
        else
            return
        end
    end
end



script.Parent.Head.Touched:connect(Own)
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Edit: I see that you've fixed your code, but you still may want to take a look at this. Using wait()/"blocking delays" isn't usually desirable in code.

Instead of using a true/false variable and wait(), try recording the last time the function was run (using time()), finding the difference between that time and the current time when the function is called, and then comparing that to a delay value. If the difference between the last time the function ran and the current time is greater than a debounce time, then run the function again and set the last ran time to the current one.

Hint:

if(time() - lastTimeRun > debounceTimeConstant)
lastTimeRun = time()
end

If you have any further questions, feel free to ask!

0
Oh I see, thank you! ProjectJager 62 — 6y

Answer this question