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)
You never defined "Mouse" in the second function where line 20 resides.
my solution was to create a variable named "mouse" and set it to nil when you equip the gun where you define "Mouse" I redefine "mouse" to equal to "Mouse"
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") local mouse = nil script.Parent.Equipped:Connect(function(Mouse) mouse = 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)