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

4 comments
On line 5. You’re putting your event inside the function, which is not okay.
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.
Inexperienced.
Whats the downside to putting events inside functions?