So I have two problems with my script first of all I get an error. The error always happens when I unequip and equip the gun again. The error says "This Mouse is no longer active". The second problem is when I unequip and equip the gun again, I get less ammo. I would be happy if you could help me error: ~~~~~~~~~~~~~~~~~
This Mouse is no longer active - Client - MainScript:24 Stack Begin - Studio Script 'Players.FireTap1.Backpack.Deagle.MainScript', Line 24 - Studio - MainScript:24 Stack End
code:
~~~~~~~~~~~~~~~~~
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
local starterGui = game:GetService("StarterGui")
script.Parent.Equipped:Connect(function(Mouse)
textLabel.Visible = true
local function reload()
mouse = Mouse
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.reload:Play()
script.Parent.gunShot:Stop()
end
while wait() do
textLabel.Text = (ammo.. " / "..maxAmmo)
end
end)
local tool = script.Parent
local anim2 = Instance.new("Animation")
local input = game:GetService("UserInputService")
anim2.AnimationId = "6425472378"
input.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
reload()
script.Parent.reload:Play()
end
end)
end)
script.Parent.Unequipped:Connect(function(Mouse)
mouse = nil
textLabel.Visible = false
end)

6 comments
Now I get an error on line 25 the error is:Players.FireTap1.Backpack.Deagle.MainScript:25: attempt to index nil with 'Target'
And I also get another error- on line 47: Players.FireTap1.Backpack.Deagle.MainScript:47: attempt to index function with 'Disconnect'
Two more things firstly I can't damage anyone anymore and when I unequip and equip again I get less ammo
The "nil" comes from looking at something without hitting an object (looking off into the distance). Ad an "if" to check for a target (if Mouse.Target) and then the following code.
For the "Disconnect" issue, it's because I forgot to assign the "connect" function to a variable (I shouldn't be using "Disconnect" for the function itself) so around line 42 say "someName = theConnectFunction" and then replace line 47 with "someName:Disconnect()"
For your "dealing damage" issue, you should probably take a look at your other script. It's probably doing something wrong.