Animation plays way too many times

I’m trying to make an attack animation play when a player gets within a zombie npc, but I don’t know how to always check if the player is within the range and play the animation without it playing a thousand times.

Not full code but the stuff that’s important

local plr = game.Players:GetPlayerFromCharacter(Goal.Parent)

RS.Stepped:Connect(function()
	if (NPC.HumanoidRootPart.Position - Goal.Position).Magnitude <= 100 then
		NPC.Humanoid:MoveTo(Goal.Position, Goal)
		setNetworkOwner(NPC, plr)
		if (NPC.HumanoidRootPart.Position - Goal.Position).Magnitude <= 7 then
			animPlay("Attack")
		end
	else
		setNetworkOwner(NPC, nil)
	end
end)

function animPlay(anim)
	if debounce == false then
		debounce = true
		anims[anim]:Play()
		task.wait(.1)
	end
	debounce = false
end

Thanks,
dza

because you put the debounce = false outside of the if statement

you should put the debounce = false inside the if statement
let’s look at an example:

  • player touch event fires
  • debounce is false, let’s play the animation
  • player touch event fires again
  • debounce is still true, but that doesn’t matter, we still set the debounce to false
  • player touch event fires again
  • now debounce is false again, so let’s play the animation again.

the touch event will fire a bunch of times as the player and zombie move and keep firing the touch event.

Your magnitude logic looks fine.
AnimationTracks have a property called IsPlaying, which you can use to your advantage here.

function animPlay(anim)
	if not anims[anim].IsPlaying then
        anims[anim]:Play()
    end
end

Edit: Changed anim to anims[anim] (???)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.