I tried making a gun and whenever I unequip the gun it says “This mouse is no longer active” and shoots 2 times instead of 1 every shot, its super annoying and I don’t know how to fix it. I looked at several forum posts at this point and still don’t know this means. Can somebody solve this? Here’s the local script:
wait(0.5)
local maxAmmo = script.Parent.MaxAmmo.Value
local Ammo = script.Parent.MinAmmo.Value
local fireRate = script.Parent.Firerate.Value
local Damage = script.Parent.Damage.Value
local reloadTime = script.Parent.ReloadTime.Value
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = script.Parent.AmmoDisplay.AmmoFrame.AmmoText
local animHold = Instance.new("Animation")
animHold.AnimationId = "rbxassetid://6524508947"
local animClick = Instance.new("Animation")
animClick.AnimationId = "rbxassetid://6524543167"
local track1
local track2
script.Parent.Equipped:Connect(function(Mouse)
track1 = script.Parent.Parent.Humanoid:LoadAnimation(animHold)
track1.Priority = Enum.AnimationPriority.Action
track1.Looped = true
track1:Play()
script.Parent.AmmoDisplay.Parent = playerGui
local function reload()
player.PlayerGui.AmmoDisplay.AmmoFrame.ReloadingText.Visible = true
script.Parent.PumpReload:Play()
reloading = true
wait(reloadTime)
player.PlayerGui.AmmoDisplay.AmmoFrame.ReloadingText.Visible = false
Ammo = maxAmmo
reloading = false
end
local changeText = coroutine.wrap(function()
while wait() do
textLabel.Text = Ammo.." / "..maxAmmo
end
end)
changeText()
script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
track2 = script.Parent.Parent.Humanoid:LoadAnimation(animClick)
track2.Priority = Enum.AnimationPriority.Action
track2.Looped = false
track2:Play()
Ammo = Ammo - 1
script.Parent.GunShot:Play()
if Mouse.Target ~= nil then
if Mouse.Target.Parent then
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, Damage)
wait(fireRate)
else
print("Stop shooting your self!")
end
end
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
end)
local Input = game:GetService("UserInputService")
Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
reload()
end
end)
end)
script.Parent.Unequipped:Connect(function()
playerGui.AmmoDisplay.Parent = script.Parent
track1:Stop()
if track2 then
track2:Stop()
end
end)
Your binding InputBegan and script.Parent.Activated inside of the equip event. you would need to disconnect the events after they are not needed anymore (aka on the unequip)
wait(0.5)
local maxAmmo = script.Parent.MaxAmmo.Value
local Ammo = script.Parent.MinAmmo.Value
local fireRate = script.Parent.Firerate.Value
local Damage = script.Parent.Damage.Value
local reloadTime = script.Parent.ReloadTime.Value
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = script.Parent.AmmoDisplay.AmmoFrame.AmmoText
local animHold = Instance.new("Animation")
local ShootEvent
local ReloadEvent
animHold.AnimationId = "rbxassetid://6524508947"
local animClick = Instance.new("Animation")
animClick.AnimationId = "rbxassetid://6524543167"
local track1
local track2
script.Parent.Equipped:Connect(function(Mouse)
track1 = script.Parent.Parent.Humanoid:LoadAnimation(animHold)
track1.Priority = Enum.AnimationPriority.Action
track1.Looped = true
track1:Play()
script.Parent.AmmoDisplay.Parent = playerGui
local function reload()
player.PlayerGui.AmmoDisplay.AmmoFrame.ReloadingText.Visible = true
script.Parent.PumpReload:Play()
reloading = true
wait(reloadTime)
player.PlayerGui.AmmoDisplay.AmmoFrame.ReloadingText.Visible = false
Ammo = maxAmmo
reloading = false
end
local changeText = coroutine.wrap(function()
while wait() do
textLabel.Text = Ammo.." / "..maxAmmo
end
end)
changeText()
ShootEvent = script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
track2 = script.Parent.Parent.Humanoid:LoadAnimation(animClick)
track2.Priority = Enum.AnimationPriority.Action
track2.Looped = false
track2:Play()
Ammo = Ammo - 1
script.Parent.GunShot:Play()
if Mouse.Target ~= nil then
if Mouse.Target.Parent then
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, Damage)
wait(fireRate)
else
print("Stop shooting your self!")
end
end
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
end)
local Input = game:GetService("UserInputService")
ReloadEvent = Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
reload()
end
end)
end)
script.Parent.Unequipped:Connect(function()
playerGui.AmmoDisplay.Parent = script.Parent
ShootEvent:Disconnect()
ReloadEvent:Disconnect()
track1:Stop()
if track2 then
track2:Stop()
end
end)
Im talking about whenever you use :Connect() you connect a function to the event (in your case InputBegan and Script.Parent.Activated)… If you connect them once and dont disconnect them it will run multiple times.
I’m pretty sure this is either a bug or a no longer continuing way of getting the mouse. Instead of the “Mouse” variable as a variable from the function, get the players mouse like this: