How Do I Make An NPC Do This

How would I make an NPC do an animation every time it gets close to a player? So like an attack animation basically. Does anyone know how to do this?

1 Like

https://developer.roblox.com/en-us/api-reference/function/Animator/LoadAnimation

if (player.Character.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude < 10 then --change 10 to how close / far you want the player to be.
--do whatever you want
end
1 Like

So I’d put something like play:anim3

1 Like
NPC:LoadAnimation(Animation):Play()
1 Like

So
if (player.Character.PrimaryPart.Position - (Npc name) .PrimaryPart.Position).Magnitude < 10 then
(NPC name):loadanimation(animation):Play()
end

Exactly, you can make a loop to make the animation play over and over but its your choice.

Just a minor nitpick, but since you have access to the player object, I suggest using

player:DistanceFromCharacter(npc.PrimaryPart.Position)

instead of Magnitude because it is faster. Do note that it can return 0 if the player’s character is nil.