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?
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.)