I have been making a gun for my game and I get this error. The error always happens when I unequip and equip the gun again.
The error: ****This Mouse is no longer active ****
It is in line 20
My Script:
local maxAmmo = 8
local ammo = maxAmmo
local reloading = false
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText")
script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if ammo > 0 and not reloading then
ammo = ammo -1
script.Parent.gunShot:Play()
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
end
elseif reloading == false then
reload()
script.Parent.gunShot:Stop()
end
while wait() do
textLabel.Text = (ammo.. " / "..maxAmmo)
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()
textLabel.Text = "Out of ammo Press R to reload"
end
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
script.Parent.reload:Play()
end
end)

2 comments
It works, but now when I unequip and equip again I get less ammo.
how much ammo do you lose?