Player stops all animations

I have been struggling with animations for tools and I always run into this problem where when the animation stops, it stops all of the animations until I move the character again. I’m not sure how to go about fixing it since I’m still fairly new to animations and scripting in overall.

Local Script:

local tool = script.Parent
local idleanimation = tool:FindFirstChild("IdleAnim")

tool.Equipped:Connect(function)
    game.ReplicatedStorage.FilteredGun.AnimationPlay:FireServer(idleanimation)
end)

tool.Unequipped:Connect(function)
    for i,v in pairs(game.Players.LocalPlayer.Character.Humanoid:GetPlayingAnimationTracks()) do
        if v.Name == "IdleAnim" then
            v:Stop()
        end
    end
end)

Server Script:

game.ReplicatedStorage.FilteredGun.AnimationPlay.OnServerEvent:Connect(Player, Animation)
    Player.Character.Humanoid:LoadAnimation(Animation):Play()
end)

What happens, in the end, is that every animation, even the default ones, stops and the player can move around stiff as a board.

Put all the animations you use for the tool in a table and stop them instead. Your code will just stop every animation playing, not exclusively the ones in your tool. :slight_smile:

You do not need to use a RemoteEvent to Load and Play animations when you are using a Humanoid. Scrap that RemoteEvent and when you call LoadAnimation, save the AnimationTrack into a table. When you want to stop all those animations, loop thru that table and call :Stop() on all of them, as intended_pun stated.

It could look something like this, all within a LocalScript:

local tool = script.Parent
local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid -- example, add in other nil checks and handlers

local animations = {
    Idle = "rbxassetid://ANIMATION_ID";
};
local animationTracks

tool.Equipped:Connect(function)
    animationTracks = {}
    -- you could add a check here to cache animations, so you dont have to reload them on each equip, but this will work.
    for animName, animId in pairs(animations) do
        local animation = Instance.new("Animation")
        animation.AnimationId = animId
        local animTrack = humanoid:LoadAnimation(animation)
        animTrack:Play()
        animationTracks[animName] = animTrack
    end
end)

tool.Unequipped:Connect(function)
    for animName, animTrack in pairs(animationTracks) do
        animTrack:Stop()
    end
    animationTracks = nil
end)
2 Likes

This is Filter Enabled compatible?

Yes. Animations replicate through the client-server boundary as long as the animation is being loaded and played within a model that is set to Player.Character. So, loading and playing animations on your own humanoid will replicate without issue.

1 Like