Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fire an animation every time a character moves, npc or player?

Okay, so I made a game which I'm not gonna really explain about and I made an animation for movement for one of the npcs. It follows the nearest player, and here's part of the code I have right now: (It's an enemy npc)

while wait() do
	local player = FindPlayer()
	if player ~= nil then
		h:MoveTo(player.Character.HumanoidRootPart.Position)
		if not walk.IsPlaying and player.Character.Humanoid.Health >= 1 then
			walk:Play()
		elseif player.Character.Humanoid.Health <= 0 then
			walk:Stop()
			player.Character.HumanoidRootPart:Destroy()
		end
	else
		h:MoveTo(spawnCF.p)
	end
end

Is there any way to detect if the character is moving, and when it does, run the animation?

4 comments

0

I fixed it up, try it now?

0

It works when It's not looped, I will now try with the animation looped Edit: It doesn't work with a loop, the animation just keeps going!

0

I won't leave you hanging, give me a bit to figure this out

0

the animation not looped looks like he's walking then stop walking but continuing to move

2 answers

✓ Accepted answer

Yes! Since both desired Characters must have a Humanoid to function properly, we can use a Signal of this Object universally to provide listeners running your Animation. This property is called Humanoid.Running, it will fire anytime the Humanoid walks. To use this, the Code should be done below.

--// For this example, we will be using the Player, it still works for NPCs though!
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local HumanoidRan

HumanoidRan = Humanoid.Running:Connect(function()
   print("Player is walking!")
   Humanoid.StateChanged:Connect(function(oldState,newState)
      if (newState == Enum.HumanoidStateType.Seated) then
         print("Doesn't meet Conditions!")
         HumanoidRan:Disconnect()
      end
   end)
end)

See Humanoid.StateChanged for a better understanding!

Hope this helps! If so, don’t forget to accept this answer!. Any more questions? Ask!

10 comments

0

Is there also a function for when the character is standing?

1

I am unaware of any sorts of Signals like so, though you can use a Conditional for this with Humanoid.Sit. This returns true if this State is true, so use reverse logic to check if they're Standing

0

if not (Humanoid.Sit) then

0

Because if there is, I want the animation to stop playing.

0

I'll edit my answer to help you out!

0

Last thing: does the animation also have to be looped?

0

It doesn't have to be, necessarily it's if you'd like it to be.

0

The NPC doesn't stop playing the walking animation even if he is standing.

0

I'll post an answer to show you my whole script, if you can notice why this happens.

0

I'm sorry if this is taking so long, but when I put your edited script in, the part HumanoidRan:Disconnect() in the script editor says "W001: Unknown global 'HumanoidRan'"

This is my whole script (it also makes the humanoid move towards the nearest player):

local h = script.Parent.Humanoid
local walkAnimation = script.Walk
local walk = h:LoadAnimation(walkAnimation)

script.Parent.Humanoid.Running:Connect(function()
	walk:Play()
	script.Parent.Humanoid.StateChanged:Connect(function(oldState, newState)
		if (newState == Enum.HumanoidStateType.Seated) then
			walk:Stop()
		end
	end)
end)

local torso = script.Parent.HumanoidRootPart
local spawnCF = torso.CFrame

script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

function FindPlayer()
	for _, v in next, game.Players:GetPlayers() do
		if v.Character then
			local char = v.Character
			if char:FindFirstChild("Humanoid") and char:FindFirstChild("HumanoidRootPart") then
				local ptorso = char.HumanoidRootPart
				if (ptorso.Position - torso.Position).magnitude <= 125 then
					return v
				end
			end
		end
	end
	return nil
end

while wait() do
	local player = FindPlayer()
	if player ~= nil then
		h:MoveTo(player.Character.HumanoidRootPart.Position)
		if player.Character.Humanoid.Health <= 0 then
			player.Character.HumanoidRootPart:Destroy()
		end
	else
		h:MoveTo(spawnCF.p)
	end
end

2 comments

0

I'll fix up my answer in a second, I am slightly busy, I will be back though!

0

By the way, the animation isn't looped, so if i loop it, it doesn't stop the animation, and when the animation isn't looped, it just plays the animation, waits a little bit and does it again

This discussion is preserved as it was. New answers, comments, and votes are closed.