Made this script but it calls the Click function even while unequipped

local Players =game:GetService("Players")
local Remotes = script.Parent.Parent.Remotes
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Values = script.Parent.Parent.Values
--Remotes
local FireRemote = Remotes.Fire
local ReloadRemote = Remotes.Reload
--GunParts
local Gun = script.Parent.Parent
local FirePart = Gun:WaitForChild("FirePart")

--GunData
local MaxAmmo = Values.MaxAmmo
local Ammo = Values.Ammo
local StoredAmmo = Values.StoredAmmo

--Debug
CanFire = nil

if not Equp then
	Gun.Equipped:Connect(function()
		CanFire = true
			print("Tool Equpied")
			Mouse.Button1Down:Connect(function()
			print(CanFire)
			print("Fire")
			if CanFire then
				FireRemote:FireServer(FirePart.Position, (Mouse.Hit.p - Gun.Handle.CFrame.Position).unit * 300)
				Ammo.Value = Ammo.Value - 1
			end
			if Ammo.Value <= 0 then
				print("Reload")
				ReloadRemote:FireServer(MaxAmmo, StoredAmmo)
			end
		end)
	end)
end
Gun.Unequipped:Connect(function()
	print("Uneq")
	CanFire = false
end)

Any Advice on fixing this?

Disconnect the function when the gun is unequipped.

1 Like

You did not make use of that “CanFire” bool in your equipped event. I would also suggest you to not put all the main gun functions into the Equipped event.

Use the Equipped event to change the “CanFire” bool to true, and Unequipped to change it to false

To make it really, really simple, just use the Tool.Activated Event to only run the gun’s function while the tool is equipped.