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

Why is debounce is not working in this Touched:Connect script?

Asked by 4 years ago
Edited 4 years ago

I am new to scripting, I tried to use debounce in this script, it does not seem to work, why? Also is my spacing/organization/neatness done correctly? Not fully sure how to order them and make the script neat.

script.Parent.Touched:Connect(function(hit)
    local players = game:GetService("Players")  --To get the service of players
    local playerHumanoid = hit.parent:WaitForChild("Humanoid")
    local hitfromPlayer = players:GetPlayerFromCharacter(hit.parent) --Getting the Player from its Character
    local debounce = false

    if not debounce then
        debounce = true


        if hitfromPlayer then
            playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed + 5

        print("work pls")
        wait (5)


        debounce = false

        end

    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Everytime you touch the part it resets the debounce, hence not making it wait for the cooldown. Put the debounce outside of the function.

local debounce = false

script.Parent.Touched:Connect(function(hit)
    local players = game:GetService("Players")  --To get the service of players
    local playerHumanoid = hit.parent:WaitForChild("Humanoid")
    local hitfromPlayer = players:GetPlayerFromCharacter(hit.parent) --Getting the Player from its Character

    if not debounce then
        debounce = true


        if hitfromPlayer then
            playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed + 5

        print("work pls")
        wait (5)


        debounce = false

        end

    end
end)
0
Oh I see, thank you WizardEpidemic 11 — 4y
Ad

Answer this question