How to keep ontouched function from debouncing

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want an animation to play when a player steps on a part
  2. What is the issue? Include screenshots / videos if possible!
    The animation debounces a few times (3) for each time the humanoid touches the part in the animation I have tried debouce but that doesnt work due to the nature of my script.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Debounce, checking if the animation is already running. I have also tried loading the animation before the function but to no avail. I have tried many things and this is one of my first scripting attempts.
local human = onTouched.Parent:findFirstChild("Humanoid")
local anim = human:LoadAnimation(script.Parent.Animation)

function onTouched(hit)
	local debounce = false
	if debounce == false then
	anim:Play()
	debounce = true
	wait(10.4166666667)
	anim:Stop()	
	debounce = false
	end
	end
script.Parent.Touched:connect(onTouched)
local Connection = script.Parent.Touched:connect(onTouched)
Connection:Disconnect()

Try using this. It seems like you were creating 2 different touched events. I also think you should define the humanoid inside the touched event, assuming this isn’t a localscript.

local Connection

Connection = script.Parent.Touched:Connect(function(Hit)
	Connection:Disconnect()
	
	local human = Hit.Parent:FindFirstChildOfClass("Humanoid")
	local anim = human and human:LoadAnimation(script.Parent.Animation)
	
	if anim then
		anim:Play()
		wait(10.4166666667)
		anim:Stop()
	end
end

If you have any issues still just reply and I’ll try my best to fix it. Although I don’t see any issues with this.

2 Likes
16:59:59 -- Workspace.Part.Animations Script:14 Expected ')' to close '(' at line 3, got <eof>
16:59:59 -- cloud_142613350.EmptyScriptAdder:7: Incomplete statement: expected assignment or a function call
16:59:59 -- cloud_142613350.EmptyScriptAdder:7: Incomplete statement: expected assignment or a function call

I got these errors and the animation didn’t play at all, I have no idea what I’m doing here.

Debouncing is done incorrectly here because if you set the local variable inside the funciton, then it’ll be reset evertime. First step, initialize it outside the function.

Then, you also are checking if it’s false, but setting it to false everytime so it’ll execute everytime the function runs. So here’s the correct usage:

local debounce = false
function doSomething()
   if not debounce then
      debounce = true
      -- do your stuff here
      debounce = false
   end
end

When I use this, the script doesn’t even run. I’m assuming that has something to do with the function, I have no idea.

Did you connect it to OnTouched? My function is just proper usage of debounce so just move around where you define your debounce and your script should run.

I’m pretty sure I did, but I’m going to bed right now. But I will check it in the morning.

add a ‘)’ to the last end my bad.

1 Like

This did fix the debounce but the script won’t run afterwards .

So you want it to run again after it plays once? If so you need to use a debounce and not :Disconnect. I misunderstood what you’re trying to do.

local playing = false

script.Parent.Touched:Connect(function(Hit)
	if playing then
		return
	end
	
	local human = Hit.Parent:FindFirstChildOfClass("Humanoid")
	local anim = human and human:LoadAnimation(script.Parent.Animation)

	if anim then
		playing = true
		
		anim:Play()
		wait(10.4166666667)
		anim:Stop()
		
		playing = false
	end
end)

My mistake, I don’t really understand how to use debounce, do you have a link to any documentation maybe? I don’t know.

Debounce isn’t an official thing, it’s just a way of stopping a script from running.

Refer to the last reply, I changed the script to use a debounce (named playing in this case). Let me know if it has the intended behavior and let me know if you have questions.

Alright, im going to try that, thanks

Just made an edit, it should be good to try now

Edit: also forgot the ) again so you may need that

1 Like