Problem with animations playing over one another

Hello developer formus!

Im having an issues determining what I should use to play the animations of my viewmodel. My goal is to have it so when I start playing a new animation, the old one stops. I have no clue why but using the :Stop() on the animation isnt working and the animations end up playing over each other and making everything appear slow and not as life like.

The script:

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character
character.Archivable = true

local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")

local characterLoaded = false

local loaded = player.CharacterAppearanceLoaded:Connect(function()
	characterLoaded = true
end)

local inventory = {
	"Assault Rifle";
	"Pistol";
}

repeat wait() until characterLoaded == true

for _,part in character:GetDescendants() do
	if part:IsA("Part") then
		part.CastShadow = false
	end
	if part:IsA("Accessory") or part:IsA("Script") then
		part.Archivable = false
	end
	if part.Name == "Right Hip" or part.Name == "Left Hip" then
		part.Archivable = false
	end
end

local currentGun = nil
local viewmodel = character:Clone()
viewmodel.Parent = camera
viewmodel.Name = "Viewmodel"
viewmodel.Head.Anchored = true
local torso = viewmodel.Torso
local leftLeg = viewmodel["Left Leg"]
local rightLeg = viewmodel["Right Leg"]
local rightArm = viewmodel["Right Arm"]

local gunBone = nil
local idleAnimation = nil
local fireAnimation = nil
local aimAnimation = nil
local reloadAnimation = nil

function loadGun(gun, typeOfGun)
	local gunFolder = game.ReplicatedStorage.Guns
	currentGun = gunFolder:FindFirstChild(gun):Clone()
	currentGun.Parent = viewmodel
	
	-- Create Bone
	gunBone = Instance.new("Motor6D")
	gunBone.Name = "Handle"
	gunBone.Parent = rightArm
	gunBone.Part0 = viewmodel["Right Arm"]
	gunBone.Part1 = currentGun.Handle
	
	-- Animation
	idleAnimation = currentGun:FindFirstChild("Idle")
	idleAnimation.Parent = viewmodel.Humanoid
	fireAnimation = currentGun:FindFirstChild("Fire")
	fireAnimation.Parent = viewmodel.Humanoid
	aimAnimation = currentGun:FindFirstChild("Aim")
	aimAnimation.Parent = viewmodel.Humanoid
	reloadAnimation = currentGun:FindFirstChild("Reload")
	reloadAnimation.Parent = viewmodel.Humanoid
end

function updatePositions()
	leftLeg.CFrame = (character["Left Leg"].CFrame - character.Head.CFrame.Position) + viewmodel.Head.CFrame.Position
	leftLeg.CFrame = leftLeg.CFrame:ToWorldSpace(CFrame.new(0,0,0))
	rightLeg.CFrame = (character["Right Leg"].CFrame - character.Head.CFrame.Position) + viewmodel.Head.CFrame.Position
	rightLeg.CFrame = rightLeg.CFrame:ToWorldSpace(CFrame.new(0,0,0))
end

local viewmodelPosition = viewmodel.Head
local lastCF = CFrame.new(0,0,0)
local swayCF = CFrame.new()
local currentSwayAMT = .6
local lastCameraCF = CFrame.new()
local target = viewmodelPosition

local reloadTimer = 0
local isAiming = false
local currentAnimation = nil

loadGun("Assault Rifle") -- Gun to spawn with
viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Play()

RunService.RenderStepped:Connect(function(delta)
	if isAiming == true then
		target = currentGun.Aim
	else
		target = viewmodelPosition
	end
	
	local rot = camera.CFrame:ToObjectSpace(lastCameraCF)
	local X, Y, Z = rot:ToOrientation()
	lastCameraCF = camera.CFrame
	
	-- Main Loop
	if characterLoaded == true then
		swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * currentSwayAMT, math.sin(Y) * currentSwayAMT, 0), .1)
		target:PivotTo(camera.CFrame * swayCF)
		updatePositions()
		--updateGun()
		if reloadTimer ~= 0 then
			reloadTimer -= delta
			if reloadTimer <= 0 then
				reloadTimer = 0
			end
		end
	end
end)

function changeGun(amount)
	currentAnimation = currentGun:FindFirstChild("Idle")
	--idleAnimation.Parent = viewmodel.Humanoid
end

function playAnimation(name)   -- Idle, Fire, Aim, Reload
	
end

UserInputService.InputBegan:Connect(function(input)
	if reloadTimer == 0 then
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			print("Right Click")
			if isAiming == true then
				isAiming = false
				--viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation):Stop()
				viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Play()
				
			else
				isAiming = true
				viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Stop()
				--viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation):Play()
			end
		end
		
		if input.KeyCode == Enum.KeyCode.R then
			reloadTimer = 2
			print("R")
			viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Stop()
			viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation):Stop()
			viewmodel.Humanoid.Animator:LoadAnimation(reloadAnimation):Play()
		end
	end
end)

And here is the game file:
Framework.rbxl (105.4 KB)

1 Like

I suck at animations so I can’t help with that. But for the janky movement, you could try to change the Humanoid’s hip height

1 Like

This is horrible practice. I want you to save the loaded animations before playing them.
So somewhere on top of the script put:

local idleAnimationTrack = viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation)
local aimAnimationTrack = viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation)
local reloadAnimationTrack = viewmodel.Humanoid.Animator:LoadAnimation(reloadAnimation)

Now at the bit of code I quoted you should check if the animation is running before you stop or run it. If you don’t it does some weird queue stuff and makes it overlapped by the same or different animations. I can’t provide an example cause I’m not near my PC and I forgot what it’s called but it should be something like:

animationTrack.IsRunning

or

animationTrack.IsPlaying

Alternatively you can stop all animations the player is running currently with:

for i,v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
	v:Stop()
end
1 Like

The last comment is great. I’ll just add a tip to create an Idle animation that doesn’t stop, so that your character doesn’t run completely without an animation, because sometimes if you run an animation and stop the others without keeping any, other players may see your character in the initial pose, the famous “T-Pose”.

1 Like

Thank you! This has been my first time coding anything pretty big without tutorials lol. I will be trying out your solutions soon!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.