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

My Touched Event keeps on firing 10 times everytime i walk over the part. What am i doing wrong?

Asked by 5 years ago

output: working x10

script.Parent.Touched:Connect(function(hit)
    local debounce = true
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and debounce == true then
        debounce = false
        local Roball = workspace.Roball1
        local animation = Instance.new("Animation")
        print('working')
        animation.AnimationId = "rbxassetid://507770239"
        local anim = Roball.Humanoid:LoadAnimation(animation)
        anim:Play()
        wait(0.3)
        debounce = true
    end
end)

2 answers

Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
5 years ago

You set the debounce to true at the startof the function.


local debounce = true function main() if debounce == true then debounce = false wait(.3) debounce = true end end
Ad
Log in to vote
1
Answered by 5 years ago

You seem to be using the concept of debounce wrong. You are initializing the variable debounce to true inside the function by stating

local debounce = true

on line 2. This means that every time the Touch event occurs, you are going to make debounce equal true. Thus the if statement will always pass because debounce will be true every time the function occurs. To solve this problem, put the line mentioned above outside the funciton like so:

local debounce = true --(Place here)
script.Parent.Touched:Connect(function(hit)
    --(Not here)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and debounce == true then
        debounce = false
        local Roball = workspace.Roball1
        local animation = Instance.new("Animation")
        print('working')
        animation.AnimationId = "rbxassetid://507770239"
        local anim = Roball.Humanoid:LoadAnimation(animation)
        anim:Play()
        wait(0.3)
        debounce = true
    end
end)

Hope that helps

Answer this question