Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

0
I fixed it up, try it now? Ziffixture 6913 — 5y
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! AradIsHere 2 — 5y
0
I won't leave you hanging, give me a bit to figure this out Ziffixture 6913 — 5y
0
the animation not looped looks like he's walking then stop walking but continuing to move AradIsHere 2 — 5y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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!

0
Is there also a function for when the character is standing? AradIsHere 2 — 5y
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 Ziffixture 6913 — 5y
0
if not (Humanoid.Sit) then Ziffixture 6913 — 5y
0
Because if there is, I want the animation to stop playing. AradIsHere 2 — 5y
View all comments (6 more)
0
I'll edit my answer to help you out! Ziffixture 6913 — 5y
0
Last thing: does the animation also have to be looped? AradIsHere 2 — 5y
0
It doesn't have to be, necessarily it's if you'd like it to be. Ziffixture 6913 — 5y
0
The NPC doesn't stop playing the walking animation even if he is standing. AradIsHere 2 — 5y
0
I'll post an answer to show you my whole script, if you can notice why this happens. AradIsHere 2 — 5y
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'" AradIsHere 2 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
I'll fix up my answer in a second, I am slightly busy, I will be back though! Ziffixture 6913 — 5y
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 AradIsHere 2 — 5y

Answer this question