"Mouse is no longer active" error

Hi, when I try to use this tool it says “Mouse is no longer active”, I never got this error and I don’T know how to fix it

This is the script in question

--//Settings
local tool = script.Parent
local ammoCount = tool:WaitForChild("AmmoCount")
local hold = false
local equipped = false

--//Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

--//Remote 
local shoot = replicatedStorage:WaitForChild("ToolRemotes"):WaitForChild("P90Remotes"):WaitForChild("Shoot")
local equip = replicatedStorage:WaitForChild("ToolRemotes"):WaitForChild("P90Remotes"):WaitForChild("Equip")
local unequip = replicatedStorage:WaitForChild("ToolRemotes"):WaitForChild("P90Remotes"):WaitForChild("Unequip")
local reload = replicatedStorage:WaitForChild("ToolRemotes"):WaitForChild("P90Remotes"):WaitForChild("Reload")


script.Parent.Equipped:Connect(function(mouse)
	equip:FireServer()
	tool.Activated:Connect(function()
		hold = true
		while hold == true do
			wait(0.1)
			shoot:FireServer(script.Parent.Barrel.Position, mouse.hit.Position, ammoCount)
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	unequip:FireServer()
end)

UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.R and ammoCount.Value ~= 50 then
		reload:FireServer(ammoCount)
	end
end)



tool.Deactivated:Connect(function()
	hold = false 
end)

You are handling tool.Activated within the Equipped handler, but you’re not disconnecting it once the tool is unequipped. Therefore every time you equip the tool, you create another handler for Activated on top of the others. Only the most recent one can use the mouse, whereas the old ones are “expired”.

So the fix for this should be to either hook up tool.Activated only once, or make sure to disconnect the handler once the tool is unequipped. Below is a fix for this. I added an activatedConnection variable to hold the connection. It’s assigned to the connection on tool.Activated and is disconnected within the Unequipped handler.

local activatedConnection

script.Parent.Equipped:Connect(function(mouse)
	equip:FireServer()
	activatedConnection = tool.Activated:Connect(function()
		hold = true
		while hold == true do
			wait(0.1)
			shoot:FireServer(script.Parent.Barrel.Position, mouse.hit.Position, ammoCount)
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	activatedConnection:Disconnect()
	unequip:FireServer()
end)
1 Like

When I try this, the code wont run again after its disconnected

activatedFunction = script.Parent.Activated:Connect(function()
    shooting = true
    while shooting do
	local humanoid = player.Character.Humanoid
	local a = math.random(-5, 5)/100
	local b = math.random(-5, 5)/100
	local c = math.random(-5, 5)/100
	humanoid.CameraOffset = Vector3.new(a,b,c)	
	ammo = ammo - 1
	script.Parent.GunShot:Play()
				
	local success, message = pcall(function()
		local mouseTarget = mouse.Target.Parent:FindFirstChild("Humanoid") or mouse.Target.Parent.Parent:FindFirstChild("Humanoid")
	end)

	if success then 
		if mouse.Target.Parent:FindFirstChild("Humanoid") or mouse.Target.Parent.Parent:FindFirstChild("Humanoid") then
			if mouse.Target.Parent:IsA("Accessory") then
				script.Parent.Damage:FireServer(mouse.Target.Parent.Parent, damage)
			else 
				script.Parent.Damage:FireServer(mouse.Target.Parent, damage)
			end
		end
	else
		print(message)
	end
	wait(shoottime)
end
end)

script.Parent.Deactivated:Connect(function()
	activatedFunction:Disconnect()
	print("disconnected")
	shooting = false
end)

script.Parent.Unequipped:Connect(function()
	activatedFunction:Disconnect()
	print("disconnected")
	shooting = false
end)