Right now, the animation plays all throughout the NPC's journey towards it's target. I need help making the Animation play once it's reached it's destination

Here's the code I have so far.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local anim = script.Parent.AnimationController:LoadAnimation(script.Parent.Animation)

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

while true do
	wait(0.1)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
			anim:Play()
	end
end

1 comment

0

There is a few ways of making this, I suggest adding a AnimPlay Value when the npc reaches the destination and then run the animation code bit when he gets the value. So it's constantly checking for it. or you can either use raycasting + magnitude, you choose.