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:

01local working = false
02local toolbox = script.Parent.Parent.Parent.Workspace.gen1.toolbox.detector
03local clickDetector = toolbox:WaitForChild("ClickDetector")
04clickDetector.MouseClick:Connect(function(Player)
05    while wait(1) do
06        local Mag = (script.Parent.toolbox.walkto.Position-Player.Character.HumanoidRootPart.Position).magnitude
07        if Mag >= script.Parent.Range.Value then
08            working = false
09            print("working = false")
10        end
11        if Mag <= script.Parent.Range.Value then
12            working = true
13            game.Workspace.gen1.working:Fire()
14            print("working = true")
15        end
16    end
17end)

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

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

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:

01local working, debounce = false, true
02local toolbox = script.Parent.Parent.Parent.Workspace.gen1.toolbox.detector
03local clickDetector = toolbox:WaitForChild("ClickDetector")
04local waitTime = 1
05clickDetector.MouseClick:Connect(function(Player)
06    while wait() do
07        local Mag = (script.Parent.toolbox.walkto.Position-Player.Character.HumanoidRootPart.Position).magnitude
08        if Mag >= script.Parent.Range.Value then
09            working = false
10            print("working = false")
11        end
12        if Mag <= script.Parent.Range.Value and debounce then
13        debounce = false
14            working = true
15            game.Workspace.gen1.working:Fire()
View all 21 lines...

second script:

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

(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