How to play an animation when an npc is moving?

I have this goalkeeper made, but when he moves I want to play an animation.

local NPC = script.Parent
local ball = workspace:WaitForChild("Ball")
local region = workspace:WaitForChild("Region")
local walking = false
local animation = script.Parent.Walking
animation.AnimationId = "rbxassetid://2510202577"
local animationTrack = game.Workspace.Goalkeeper.Humanoid:LoadAnimation(animation)

while true do
	local ballPos = ball.Position
	local regionPos = region.Position

	if ballPos.Z < regionPos.Z - region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2) * CFrame.Angles(0, math.rad(-90), 0)
	elseif ballPos.Z > regionPos.Z + region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z + region.Size.Z/2) * CFrame.Angles(0, math.rad(-90), 0)
	else
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, ballPos.Z)
	end
	wait(0.05)
end


Thanks

2 Likes

Use Humanoid.Running.

By the way, it looks like you are manually editing the CFrame to move your NPC. You don’t need to do this, instead you can use Humanoid:MoveTo()

1 Like

Humanoid:MoveTo(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2) * CFrame.Angles(0, math.rad(-90), 0)

^^ Is that what I type?

2 Likes

And by the way, it’s not really an npc, it’s a goalkeeper that tried to move infront to the ball. It doesn’t normally walk there, but I want to play the animation to make it look real.

2 Likes

Use Humanoid:Move(position). Humanoid | Roblox Creator Documentation

humanoid:Move(Vector3.new(0, 0, -1))

After using Humanoid:Move(), you should be able to use Humanoid.Running to play and stop your animation. The code might look like this:

humanoid.Running:Connect(function(speed)
    if speed > 0 then
        anim:Play()
    else
        anim:Stop()
    end
end)

Yes, but I don’t want to to just walk to a certain position, what I have now is what I want. I need to know how to implement animation:Play() into my code.

2 Likes

I see. Well, you could just play the animation whenever you edit the CFrame.

Yes, but it bugs. It will play it again every 0.05 seconds.

2 Likes

Only play it if not already playing.

for example i would do either of the following things:

  1. make a folder for the animation and put the animation in it
    then when you want the animation to play i would just do
script.Parent.FolderName.Animation:Play()
  1. or i would use instance.new("")
    make new animation, and make the anim play when i need to. this might be a bit difficult tho so yea
1 Like

Yeah, I’ve got the animation set up… But how do I play it without it playing again every 0.05 seconds?

2 Likes

well you can use this

while wait(.5) do
script.Parent.FolderName.Animation:Play()
end

Now it will play it every 0.5 seconds. still bugging it

4 Likes

Same here I got it fixed though

1 Like

and how? could you tell me how you fixed it?

3 Likes

oh did i make a mistake in my small lines of script?
could you tell the what didn’t work so i can improve

Don’t mind that user, they’ve been sending me death threats for 2 days now.

3 Likes

shouldn’t you report them to roblox?
if thats true you should reach out to the devforum admins

1 Like

Already did. They just give the standard message and probably don’t do anything

3 Likes