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

NPC Animation script fix and improvements?

Asked by
Rhidlor 42
5 years ago

So the goal of the following script is to add running and idle animations for all the enemy npcs in my game. The core of the script works, but sometimes the running animation stops while the enemy is still moving. I'm using the default roblox run and idle animations

If you can spot the source of my problem, or see any other room for improvement, I'd greatly appreciate the help!

function RegisterEventAnimations(Enemy)
    local Humanoid = Enemy.Humanoid
    Humanoid:LoadAnimation(script.Idle):Play()
    local RunAnimation = Humanoid:LoadAnimation(script.Run)
    Humanoid.Running:Connect(function(Speed)
        if Speed > 0.5 and not RunAnimation.IsPlaying then
            RunAnimation:Play()
        elseif RunAnimation.IsPlaying then
            RunAnimation:Stop()
        end
    end)
end

for _, Enemy in pairs(game.Workspace.Enemies:GetChildren()) do
    if Enemy:IsA("Model") and Enemy:FindFirstChild("Humanoid") then
        RegisterEventAnimations(Enemy)
    end
end
0
On line 5. You’re putting your event inside the function, which is not okay. User#19524 175 — 5y
0
Interesting, what is the drawback? I'm only doing it this way because I've seen multiple people on here with 2000+ rep do it. Rhidlor 42 — 5y
0
Inexperienced. User#19524 175 — 5y
0
Whats the downside to putting events inside functions? Rhidlor 42 — 5y

Answer this question