NPC Path Finding Speed

So I was just getting into NPC Pathfinding and all I really did was search up on google pathfinding and copy pasted the code. I tried to understand it and I guess maybe I get it a little. The problem I’m encountering now is that I need animations on my NPC. So I did what I did and did a little scripting and couldn’t really seem to get it to work. The problem I was having is that the I don’t know how to get the speed of the NPC when he is pathfinding to the part. I tried using a running function it didn’t really seem to work and also changing the WalkSpeed of the humanoid while he is on his way to the part does not do anything.
Here Is My Script:


local monster = script.Parent
local rootpart = monster.HumanoidRootPart
local humanoid = monster.Humanoid
local walk = script.walk
local run = script.run
local idle = script.idle

local walktrack = humanoid:LoadAnimation(walk)
local runtrack = humanoid:LoadAnimation(run)
local idletrack = humanoid:LoadAnimation(idle)
wait(5)

humanoid.Running:Connect(function(speed)	
	if speed > 14 then
		print("e")
		idletrack:Stop()
		walktrack:Stop()
		runtrack:Play()
	else
		print("e2")
		idletrack:Stop()
		runtrack:Stop()
		walktrack:Play()
	end
end)



ps: you can ignore the wait i just put it in there so i can see while it happens
ps2: i dont want you to create my a script but rather tell me how to check a humanoid’s speed constantly (walkspeed is a set amount and is not the ‘Current Speed’)
edit: the pathfinding is in another script

1 Like

Try this (I’m not sure it works 100% but test it and respond back)

local RunService = game:GetService("RunService")
local monster = script.Parent
local rootpart = monster.HumanoidRootPart
local humanoid = monster.Humanoid
local walk = script.walk
local run = script.run
local idle = script.idle

local walktrack = humanoid:LoadAnimation(walk)
local runtrack = humanoid:LoadAnimation(run)
local idletrack = humanoid:LoadAnimation(idle)
wait(5)
RunService.Heartbeat:Connect(function()
   local speed = humanoid.MoveDirection.Magnitude	
   if speed > 14 then
		print("e")
		idletrack:Stop()
		walktrack:Stop()
		runtrack:Play()
	else
		print("e2")
		idletrack:Stop()
		runtrack:Stop()
		walktrack:Play()
	end
end)
1 Like

You might also need to create an “Animator” and load the tracks into there in order for them to play:

local newAnimator = Instance.new("Animator")
newAnimator.Parent = humanoid

local walktrack = newAnimator :LoadAnimation(walk)
local runtrack = newAnimator :LoadAnimation(run)
local idletrack = newAnimator :LoadAnimation(idle)

2 Likes

This is true as well, while LoadAnimation on humanoid works it is deprecated and you should use it on an animator

1 Like

thank you guys for your response I do actually have a animator object
image
so i will just change humanoid to animator. also sorry for the late reply i am only active for a few minutes i will be more active in 20 minutes. Thanks!

1 Like

The animations have stopped working completely I dont know if this is related from this script because they stopped working before i copied the script. I will be more active in 20 minutes
edit: the animations have stopped working but the print works it prints 1k times because of the heartbeat so the function is good it is now about the animations loading

1 Like