Weapon Hold/Fire Animations Not Playing Correctly

I currently have a tool, and they are animated with Motor6Ds. I have a viewmodel for the first person, and proper animations for third person.

The equip, hold and aiming animations all work correctly. However, firing while aiming will break the animations, causing the viewmodel to be stuck in the aim holding animation, and not backing out even when I release right click.

“Steelsight” stands for aiming and “hold” stands for holding animations.

UserInputService.InputBegan:Connect(function(input)
	if script.Parent.Parent:FindFirstChildOfClass("Humanoid") then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			firing = true
			if steelsight then
				load = animator:LoadAnimation(anim_steelsight_fire)
			else
				load = animator:LoadAnimation(anim_fire)
			end
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
		elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
			steelsight = true
			load = animator:LoadAnimation(anim_steelsight)
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
			
			load = animator:LoadAnimation(anim_steelsight_hold)
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if script.Parent.Parent:FindFirstChildOfClass("Humanoid") then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			firing = false
			load:Stop()
		elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
			steelsight = false
			load:Stop()
		end
	end
end)

I’m thinking when you aim, you set load to the aim animation but when you shoot + aim you set the load to shoot animation meaning the aim animation never finishes playing. I suggest the animations in variables to fix this.

That seems to be the problem. Thank you!
I’ve fixed this problem by stopping the animation:

UserInputService.InputBegan:Connect(function(input)
	if script.Parent.Parent:FindFirstChildOfClass("Humanoid") then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			firing = true
			load:Stop()
			if steelsight then
				load = animator:LoadAnimation(anim_steelsight_fire)
				load.Priority = Enum.AnimationPriority.Action
				load:Play()

				load = animator:LoadAnimation(anim_steelsight_hold)
			else
				load = animator:LoadAnimation(anim_fire)
			end
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
		elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
			steelsight = true
			load:Stop()
			load = animator:LoadAnimation(anim_steelsight)
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
			
			load = animator:LoadAnimation(anim_steelsight_hold)
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if script.Parent.Parent:FindFirstChildOfClass("Humanoid") then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			firing = false
			load:Stop()
			
			if steelsight then
				load = animator:LoadAnimation(anim_steelsight_hold)
				load.Priority = Enum.AnimationPriority.Action
				load:Play()
			else
				load = animator:LoadAnimation(anim_hold)
				load.Priority = Enum.AnimationPriority.Action
				load:Play()				
			end
			
		elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
			steelsight = false
			load:Stop()
			load = animator:LoadAnimation(anim_steelsight)
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
			load.TimePosition = load.Length
			load:AdjustSpeed(-1)

			load = animator:LoadAnimation(anim_hold)
			load.Priority = Enum.AnimationPriority.Action
			load:Play()
		end
	end
end)

I think the spam loaded animations pile up inside the animator and this might cause issues… Maybe? What do you lose by storing them in variables? Its only like 4 animations :skull:

I do store them in variables:

local animations = Instance.new("Folder", tool)
animations.Name = "AnimationsFPS"

local anim_equip = Instance.new("Animation", animations)
anim_equip.Name = "Equip"
anim_equip.AnimationId = "rbxassetid://" .. data.animations_fps.equip

local anim_hold = Instance.new("Animation", animations)
anim_hold.Name = "Hold"
anim_hold.AnimationId = "rbxassetid://" .. data.animations_fps.hold

local anim_steelsight = Instance.new("Animation", animations)
anim_steelsight.Name = "Steelsight"
anim_steelsight.AnimationId = "rbxassetid://" .. data.animations_fps.steelsight

local anim_steelsight_hold = Instance.new("Animation", animations)
anim_steelsight_hold.Name = "SteelsightHold"
anim_steelsight_hold.AnimationId = "rbxassetid://" .. data.animations_fps.steelsight_hold

local anim_fire = Instance.new("Animation", animations)
anim_fire.Name = "Fire"
anim_fire.AnimationId = "rbxassetid://" .. data.animations_fps.fire

local anim_steelsight_fire = Instance.new("Animation", animations)
anim_steelsight_fire.Name = "SteelsightFire"
anim_steelsight_fire.AnimationId = "rbxassetid://" .. data.animations_fps.steelsight_fire

Or should I be storing the loaded animation in the variable?

Yes, store the loaded animations in a variable. I personally prefer keeping them in a table.

If you were doing it because of too many variables then you can use a function. Here’s an example function I used:

Handler.LoadAnimation = function(Character:Model, AnimId:number) : AnimationTrack?
	if not Character or not AnimId then return end	
	
	local Hum = Character:FindFirstChildOfClass("Humanoid")
	if not Hum or Hum:GetState() == Enum.HumanoidStateType.Dead then return end
	
	local Animator = Hum:FindFirstChildOfClass("Animator")
	if not Animator then return end
	
	local Anim = Instance.new("Animation")
	Anim.AnimationId = `rbxassetid://{AnimId}`
	
	local Track = Animator:LoadAnimation(Anim)
	
	Anim:Destroy()
	Anim = nil
	
	return Track
end	

After this I would do:

local RandomAnimation = Module.LoadAnimation(Character, ID)
RandomAnimation:Play()
1 Like

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