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

Debounce does not work as intended with .Touched event?

Asked by 2 years ago
Edited 2 years ago

Currently trying to make a combat system with .Touched and having trouble "debouncing" with the event.

script.Parent.Activated:Connect(function()
    if not debounce then
        debounce = true
        if combo == 0 then
            combo = 1
            newHitbox = hitbox:Clone()
            newHitbox.Parent = myHumanoidRootPart
            newHitbox.CFrame = myHumanoidRootPart.CFrame
            debris:AddItem(newHitbox, 0.9)

            local weld = Instance.new("Weld")
            weld.Parent = newHitbox
            weld.Part0 = newHitbox
            weld.Part1 = myHumanoidRootPart

            newHitbox.Touched:Connect(function(hit)
                if hit.Parent:FindFirstChild("Humanoid") then
                    print("Hit")
                end
            end)

            combo = 0
        end
        debounce = false
    end
end)

I've tried adding a debounce within the touched event itself and that didn't work either.

It'll essentially detect all the players' body parts pritning "Hit" 7 times rather than 1.

2 answers

Log in to vote
0
Answered by 2 years ago

You just need to add a wait(duration) before setting the debounce back to false. Currently, you're setting the debounce to true then almost immediately setting it back to false.

Ad
Log in to vote
0
Answered by 2 years ago

To build upon what Crimsonicacy said, you need to add a wait to it because as soon as the event fires, you're setting it to true then immediately back to false. Also, however, you need to move the debounce into touched event itself, or setup a second for the touched event, as the debounce outside of it won't have any effect on it as is meaning it is still reading it as every single part that touches it will pass through.

0
Thanks for the full explanation, I didn't want to accept your answer however since the other person was first. OffMyGrinds -5 — 2y

Answer this question