Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.