Animation Continues to Play?

Hello,

So This is an Aiming System I made for a Weapon,
It works, But sometimes when you either:

  • You Lose Focus of the Game (Go to another Window)

  • Or By Random Chance when using the Weapon

The Aiming Animation continues to Play, and when Unequipping, It continues to play the Animation over the others.

More Specifically I’m talking about the InputBegan and InputEnded parts.

How Should I fix this with the Current Code?


AimKey is Q
MouseKey is Button2 (Right Mouse Button)
Aiming & Active is a Boolean


(Sorry for the Overwhelming amount of code, I Added Comments to Help)

task.spawn(function() -- coroutine
	while true do -- loop
		local Distance = (workspace.CurrentCamera.CFrame.Position - Character.Head.Position).Magnitude -- For Camera's Distance
--[[ Not Important Part
		if Tool:GetAttribute("Shooting") then 
			firetrack = Character.Humanoid:LoadAnimation(Fire)
			firetrack:Play()
			AnimController:LoadAnimation(Fire2):Play()
			Events.FireWeapon:FireServer(Tool, Damage, Range, Mouse.Hit.Position, script.Sound_Fire)
		end
]]
		task.wait(Firerate)
		
		if Distance > 2 and Tool:GetAttribute("Aiming") then -- Removes Aiming Effect if Zoom is bigger than 2
			Weapon:RestoreDefaults(Player)
			Tool:SetAttribute("Aiming", false)
			Events.Aim:FireServer(Tool:GetAttribute("Aiming"), Idle, Fire, IdleId, FireId)
			if Aimingtrack then
				Aimingtrack:Stop()
			end
		end
	end
end)



InputEnter = Weapon.UserInput.InputBegan:Connect(function(Input, gameProcessedEvent)
	
	local Distance = (workspace.CurrentCamera.CFrame.Position - Character.Head.Position).Magnitude -- Also Zoom
	
	
	if Tool:GetAttribute("Active") then -- If  Weapon is Equipped
		if Input.KeyCode == Weapon.AimKey or Input.UserInputType == Weapon.MouseKey then
			Aimingtrack = Character.Humanoid:LoadAnimation(Aim) -- Aiming track
			if not gameProcessedEvent and Distance < 1 then -- if Zoom is less than 1
				Tool:SetAttribute("Aiming", true) 
				
				Weapon:Scope(Player, workspace.CurrentCamera, "ScopedRifle", Tool:GetAttribute("Aiming")) -- Scopes Player
				Events.Aim:FireServer(Tool:GetAttribute("Aiming"), Idle, Fire, A_IdleId, A_FireId) -- Sets Global
				Aimingtrack:Play() -- Plays Animation
			end
			
		end
	end
end)

InputEnd = Weapon.UserInput.InputEnded:Connect(function(Input, gameProcessedEvent)
	if Tool:GetAttribute("Active") then
		
		if Input.KeyCode == Weapon.AimKey or Input.UserInputType == Weapon.MouseKey then
			if not gameProcessedEvent then
				Tool:SetAttribute("Aiming", false) -- Removes Aim
				Weapon:Scope(Player, workspace.CurrentCamera, "ScopedRifle", Tool:GetAttribute("Aiming")) -- Removes Scope
				Events.Aim:FireServer(Tool:GetAttribute("Aiming"), Idle, Fire, IdleId, FireId) -- Sets Global
				if Aimingtrack then -- If Aimingtrack is Playing
					Aimingtrack:Stop() -- Stops Animation
				end
--[[ Useless
				if firetrack then 
					firetrack:Stop() 
				end
]]
			end
		end		
	end
end)

It might be an easy fix.

Checking the Playing tracks, there is a Animation called Aim which is the Aiming Animation. I’m not sure how to Stop it, or Remove it without removing all tracks

Actually, I Fixed it:

if Aimingtrack and not Tool:GetAttribute("Aiming") then
			local T = Character:FindFirstChildOfClass("Humanoid"):GetPlayingAnimationTracks()
			for e,i in pairs(T) do
				if tostring(i) == "Aim" then
					i:Stop()
				end
				
			end
			
			Aimingtrack = nil
		end

Let me know if there is a better way

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