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.