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?