I have an onTouched function that runs a looped animation once a player touches a part, and when the player stops touching the part it stops the animation. How exactly would I use debounce to run the looping animation each time a player touches a part?

My poor attempt:

local deb = false
script.Parent.UpperTorso.Touched:Connect(function(hit)
	if not deb then
		deb = true
		local player = game:GetService("Players"):GetPlayerFromCharacter(script.Parent)
		local character = player.Character
		local humanoid = character.Humanoid
		local head = character:WaitForChild('Head')
		local falling = head:FindFirstChild('FreeFalling')
		local storage = game:GetService("ReplicatedStorage")
		local Animation = Storage.Animations:FindFirstChild('Treading')
		local Track = humanoid:LoadAnimation(Animation)
			if hit.Name == 'Ocean' and Track.IsPlaying == false then
				Track:Play()
		end

		if Track.IsPlaying == true then
			humanoid.WalkSpeed = 0
		else
			humanoid.WalkSpeed = 16
		end
		if falling.IsPlaying == true then
			falling.Looped = false
			falling:Stop()
		else
			return
		end
	end
end)

The code above only executes once, so when the player stops touching the part then touches it again it doesn't execute. How would I get it to execute each time a player touches it; without the animation spamming?

3 comments

1

I don't know if this is the awnser your looking for BUT you could change debounce back to true after they have stopped stepping on it. So You could see if Hit.parent isn't the local players name and if it isn't change it to false.

0

I'll be sure to try this out right away, thanks for the suggestion!

0

Thanks man that was exactly what I needed, worked great!