How do you check if an npc is moving or not?

The game I’m making I want animations to play on the client. But I don’t wanna use a remote event to handle this specific task. MoveDirection won’t work in this case, so what should I do?

1 Like
local npc = game.ReplicatedStorage.YourNpc 
local humanoid = npc.Humanoid

if humanoid.Walkspeed > 0 then
     --Code goes here

else if humanoid.Walkspeed < 0 then
    --Code goes here

When a player’s not moving their walkspeed stays the same. The same can be said for Npcs, so that won’t fix the issue.

local npc = PATH_TO_NPC
local npcHumanoid = npc:FindFirstChildOfClass("Humanoid")

if npcHumanoid.MoveDirection.Magnitude > 0 then
 -- walking
else
 -- not walking
end

-- other method:
local velocity = npcHumanoid.AssemblyLinearVelocity
if Vector3.new(velocity.X, 0, velocity.Z).Magnitude > 0 then
 -- walking
else
 -- not walking
end

I tried both methods, but it didn’t change much. The first method was more effective because it detected the npc not moving, and played the idle animation. The second one didn’t detect neither which is kind of weird.

Here’s the code:

while task.wait(0.1) do
	for i,v in pairs(npcFolder:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			local Animations = v:FindFirstChild("Animations")
			local NpcHumanoid = v.Humanoid
			local NpcVelocity = NpcHumanoid.AssemblyLinearVelocity
			local animTrack
			if Vector3.new(NpcVelocity.X, 0, NpcVelocity.Z).Magnitude > 0 then
				animTrack = playAnimation(v, "Running")
			else
				animTrack = playAnimation(v, "Idle")
			end
			if animTrack then
				animTrack:Play()
			end
		end
	end
end

I’m moving the npc on the server, and i’m checking if it’s moving on the client. Do you think this is the reason for why it’s not working?

Since you are trying this on the client, try this:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAddded:Wait()

character.Humanoid.Move:Connect(function()
-- runs repeatedly when the character is moving
end)

I tried to move the Npc on the client too, but it didn’t fix anything. Idk why this is such a mess to figure out I’ve been trying to fix this issue for 2 hours Roblox what are you doing?

Note that it CONTINOUSLY and REPEATEDLY runs while the humanoid is moving.

Oh, uh that looks like it would work but what about detecting whether or not the players not moving too?

You mean to randomly check if they’re moving or not at a random moment? Here’s an idea.

local humanoidRootPart = character.HumanoidRootPart

if humanoidRootPart.Velocity.Magnitude>0 then
    -- is moving
else
    -- is not moving
end

will look something like this

local NPCOLDPOSITION
	while true do 
	if (NPC.BODYPART.Position - NPCOLDPOSITION).Magnitude > 1 then
		print("They have moved more than a stud :O")
	end
	NPCOLDPOSITION = NPC.BODYPART.Position
	wait(1)
end

That works in that sense, but I don’t think that’s what they want. Also, someone could move then move back, and it would not detect it.

if you reduce the wait this is less likely and if movements are that miniscule does it matter? questions questions context context

can you be more specific on whats going on in your game why u need to know if an NPC is moving or has just moved @Wyzloc

@Wyzloc I tested out the code I sent you (the second one). The results:
https://gyazo.com/1ba9ae938c9d4a4d260c5bddf59532f2

Have you tested it on an Npc yet? I’d try testing it multiple times because for me it did work, but sometimes the Npcs would be running in place.

@S0MBRX I was specific all I’m asking is how to check if an Npc is moving or not so I can make them play running animation when they are and idle animation when they arent on the Client.

Do the NPCs have HumanoidRootParts?

Indeed they do they’re just R15 Rigs after all.

make sense yeah vinnys way works but also why dont you just run the animations when you make the NPC’s move in the first place?