Help with stopping a weird firing issue when reloading a weapon

Essentially, I have a gun script. However, I have this odd issue, where if the player unequips while reloading, upon reequipping, the gun fires two shots. Repeat that, and it fires three shots, and so on.
I have no idea what is causing this, please help.
Reload;

local function reload(plr)
	--//Initialize
	if not canReload() then return end

	--//Reload
	reloading = true
	reloadSound:Play()
	local bruh = script.Parent.Parent.Pistol.Mag

	bruh.Material = "Plastic"

	
	while ammo.Value ~= magSize.Value do
		wait(.125)
		if equipped == false then
			break
		end
		ammo.Value += 1
		
	end


	
	
	--//Handle ammo
	ammo.Value = magSize.Value
		reloading = false


end

Firing system:

local function reload()
	if canReload:InvokeServer() then
		--//Reload
		reloadRemote:FireServer()
		
		if reloadTrack then
			reloadTrack:Play()
		end
	end
end

local function fire()
	--//Checks
	if canShoot:InvokeServer() then
		--//Initialize
		
	
		
		--[[local SPREAD = 1000 -- set some value for it
		local spreadPosition = Vector3.new(
			hole.CFrame.p.X + math.random(-SPREAD, SPREAD)/1000,
			hole.CFrame.p.Y + math.random(-SPREAD, SPREAD)/1000,
			hole.CFrame.p.Z + math.random(-SPREAD, SPREAD)/1000
		)]]
		local ray = Ray.new(hole.CFrame.p, (mouse.hit.p - hole.CFrame.p).unit * range.Value)
		local touch, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		

		--//Hit detection
		if touch then
			hitRemote:FireServer(touch)
		end
		shootRemote:FireServer()
		
		--//Trace
		if allowTracing.Value then
			remotes.Oof:FireServer(hole.CFrame.p, mouse.hit.p
			)
			--//Create
			local trace = Instance.new("Part")
			trace.Anchored = trace
			trace.CanCollide = false
			trace.Transparency = .5
			trace.BrickColor = BrickColor.new("Medium blue")
			trace.Material = Enum.Material.Neon
			
			--//Calculate
			local distance = (hole.CFrame.p - position).magnitude
			trace.Size = Vector3.new(.1, .1, distance)
			trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
			trace.Parent = workspace
			
			--//Clean-up
			game:GetService("Debris"):AddItem(trace, 0.02)
			
			wait(.02)
			
		
		end
	end
end

Thanks in advance!

Can you share the code which handles connecting the tool’s ‘Activated’ signal (event)?

2 Likes

Have you tried using debounce? That helped with one of my scripts earlier but I think it could help in this situation, not sure though.