Gun system ammo issue

Trying to make a gun system and when you shoot it subtracts one ammo

Every time you reselect it subtracts one more ammo
*equip subtracts 1, deselect and equip again subtracts 2, so on and so forth.

local maxAmmo = 24
local ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local textLabel = PlayerGui:WaitForChild("GunUI"):WaitForChild("AmmoText")
local gunui = PlayerGui:WaitForChild("GunUI")
local mouseui = game.Players.LocalPlayer:GetMouse()

-- 6704039473
-- 46537754

script.Parent.Equipped:Connect(function(mouse)
	mouse.Icon = 'rbxassetid://6231821602'
	gunui.Enabled = true
	local function reload()
		reloading = true
		script.Parent.Reload:Play()
		wait(1)
		ammo = maxAmmo
		reloading = false

	end

	script.Parent.Activated:Connect(function()
		if ammo > 0 and not reloading then
			ammo = ammo - 1
			script.Parent.Barrel.Smoke.Enabled = true
			script.Parent.Fire:FireServer()
			wait(0.1)
			script.Parent.Barrel.Smoke.Enabled = false
			if mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.Damage: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)
end)

.

You connect the function every time the tool is equipped. Make sure to disconnect it on unequip.

How would I do that with this function?

Actually, that’s completely useless. Disconnecting doesn’t do anything, the event will still fire regardless.
(Unless you only wanted it to fire one time, in which you would use :Wait()).

Generally this is how you disconnect events

local equipConn = nil
equipConn = tool.Equipped:Connect(function()
    tool.Unequipped:Connect(function()
        equipConn:Disconnect()
    end)
end)

Event will fire but script will only run function once instead of multiple times.

Oh… I misread the code, sorry!
The solution might be to just put it outside of the :Equipped event (Tool.Activated)

1 Like

No idea if I did this right but it’s doing the same thing

local equipConn = nil

conn = script.Parent.Equipped:Connect(function(mouse)
	mouse.Icon = 'rbxassetid://6231821602'
	gunui.Enabled = true
	local function reload()
		reloading = true
		script.Parent.Reload:Play()
		wait(1)
		ammo = maxAmmo
		reloading = false
		
	end
	
	script.Parent.Unequipped:Connect(function()
		equipConn:Disconnect()
	end)
	
	script.Parent.Activated:Connect(function(shoot)
		if ammo > 0 and not reloading then
			ammo = ammo - 1
			script.Parent.Barrel.Smoke.Enabled = true
			script.Parent.Fire:FireServer()
			wait(0.1)
			script.Parent.Barrel.Smoke.Enabled = false
			if mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.Damage: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 then
			reload()
		end
	end)
end)

Think they meant for you to do this

local Input = game:GetService("UserInputService")
local equipConn = nil
local reloadConn = nil

local function reload()
	reloading = true
	script.Parent.Reload:Play()
	wait(1)
	ammo = maxAmmo
	reloading = false
end

script.Parent.Equipped:Connect(function(mouse)
	mouse.Icon = 'rbxassetid://6231821602'
	gunui.Enabled = true
	
	equipConn = script.Parent.Activated:Connect(function(shoot)
		if ammo > 0 and not reloading then
			ammo -= 1
			script.Parent.Barrel.Smoke.Enabled = true
			script.Parent.Fire:FireServer()
			wait(0.1)
			script.Parent.Barrel.Smoke.Enabled = false
			if mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.Damage:FireServer(mouse.Target.Parent, 20)
			end
			textLabel.Text = (ammo).."/"..maxAmmo
		elseif not reloading then
			reload()
			script.Parent.GunShot:Stop()
		end
	end)
	
	reloadConn = Input.InputBegan:Connect(function(Key, gpe)
		if Key.KeyCode == Enum.KeyCode.R and not reloading and not gpe then
			reload()
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	equipConn:Disconnect()
	reloadConn:Disconnect()
end)

You don’t need all those events in the equipped function, the unequipped should be outside

1 Like

Yikes, I made a typo. You’re supposed to write equipConn = , not conn =

I edited message, now its fixed

Thanks I really appreciate it!

1 Like

Glad to be of help to you! If you have anymore issues don’t be afraid to make another post!

1 Like