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

How do I debounce this script that uses a bindable event?

Asked by 6 years ago
Edited 6 years ago

I am making a script so when a player clicks a particular part it increases variable 'progress' whilst they are within range of the part. The first script is here:

local working = false
local toolbox = script.Parent.Parent.Parent.Workspace.gen1.toolbox.detector
local clickDetector = toolbox:WaitForChild("ClickDetector")
clickDetector.MouseClick:Connect(function(Player)
    while wait(1) do
        local Mag = (script.Parent.toolbox.walkto.Position-Player.Character.HumanoidRootPart.Position).magnitude
        if Mag >= script.Parent.Range.Value then
            working = false
            print("working = false")
        end
        if Mag <= script.Parent.Range.Value then
            working = true
            game.Workspace.gen1.working:Fire()
            print("working = true")
        end
    end
end)

The script that increments the value when the event is fired is here:

local progress = 0
game.Workspace.gen1.working.Event:Connect(function()
    wait(1)
    progress = progress + 1
    print(progress)
end)

My problem is that the player can just click click detector infinitley spamming the event to update progress by one, how can I debounce this?

1 answer

Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
6 years ago
Edited 6 years ago

This should work:

local working, debounce = false, true
local toolbox = script.Parent.Parent.Parent.Workspace.gen1.toolbox.detector
local clickDetector = toolbox:WaitForChild("ClickDetector")
local waitTime = 1
clickDetector.MouseClick:Connect(function(Player)
    while wait() do
        local Mag = (script.Parent.toolbox.walkto.Position-Player.Character.HumanoidRootPart.Position).magnitude
        if Mag >= script.Parent.Range.Value then
            working = false
            print("working = false")
        end
        if Mag <= script.Parent.Range.Value and debounce then
        debounce = false
            working = true
            game.Workspace.gen1.working:Fire()
            print("working = true")
        wait(waitTime)
        debounce = true
        end
    end
end)

second script:

local progress = 0
game.Workspace.gen1.working.Event:Connect(function()
    progress = progress + 1
    print(progress)
    --adding a wait in this code won't do any thing besides delay the code.
end)

(Sorry that my first awnser wasn't what you wanted. I misunderstuud your question.)

0
Running this code shows only working being printed to the output, meaning it isn't running when working = true Marty999 82 — 6y
0
Oh you wanted a debounce for your first script? Hold on, I'll edit the reply. jaschutte 324 — 6y
0
Haha sorry I thought that was for the first script not the second, got variable names mixed up. Don't bother. It works fine. Thank you so much! :) Marty999 82 — 6y
0
Oh well, I still did it xD. This should also work, it is up to you if you want to change it. jaschutte 324 — 6y
0
Thank you, I think I will use it :) Marty999 82 — 6y
Ad

Answer this question