Stop firing when gun is unequipped

I am trying to make a fps game and I just found out that when I unequip my gun it still fires after I unequipped it

after I unequipped the tool the autofire also stopped working.

this is the code:

local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local shootDelay = 1 / 12
local LastFired = 0
local held = false

local FireKey = Enum.UserInputType.MouseButton1

local players= game:GetService("Players")
local client = players.LocalPlayer
local cursor = client:GetMouse()

local function fire()
	local now = tick()
	if now - LastFired >= shootDelay then
		LastFired = now
		remote:FireServer(cursor.Hit.Position)
	end
end
script.Parent.Equipped:Connect(function()
	
	UserInputService.InputBegan:Connect(function(i, gp)
		if not gp and i.UserInputType == Enum.UserInputType.MouseButton1 then
			fire()
			held = true
		end
	end)

UserInputService.InputEnded:Connect(function(i, gp)
		if not gp and i.UserInputType == Enum.UserInputType.MouseButton1 then
			held = false
		end
	end)
end)

connection = RunService.RenderStepped:Connect(function()
	if held then
		fire()
	end
end)

script.Parent.Unequipped:Connect(function()
	held = false
	connection:Disconnect()
end)

Does anyone know what is wrong?

The fire() function is being ran everytime you click the left mouse button

UserInputService.InputBegan:Connect(function(i, gp)
		if not gp and i.UserInputType == Enum.UserInputType.MouseButton1 then
			fire() --This line is causing the problem
			held = true
		end
	end)

As a result, even when you unequip the tool, it’ll always fire when you have the tool unequipped

Nevermind I forgot: (Edit: I didn’t forget it I just put it in the wrong place)

tool.Equipped:Connect(function()

thanks anyways! (character limit)

The “Equipped” event is only ever connected to a function once.