The mouse is not active error

local tool = script.Parent

tool.Equipped:Connect(function(mouse)
    while wait() do
        if mouse and mouse.Target then
            print(mouse.Target)
        end
    end
end)

if i keep unequipping and equipping, it will show an error :

  14:21:54.977 - This Mouse is no longer active```

any way to fix?
1 Like

I believe this has to do with tool.Equipped creating a new mouse for every equip and destroying it afterwards. And you should use player:GetMouse which doesn’t have this issue

local tool = script.Parent
local cursor = game:GetService("Players").LocalPlayer:GetMouse()
local UserInputService = game:GetService("UserInputService")

tool.Equipped:Connect(function()
    local connection
    connection = UserInputService.InputChanged:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseMovement then
            print(cursor.Target)
        end
        connection:Disconnect()
    end)
end)

I used UserInputService for tracking mouse movement, which might be a little overkill but imo good production code shouldn’t use the mouse instance for that stuff

1 Like

You should use RunService and wait for the event to fire instead of wait().

One easy way to do it is to just connect to the Unequipped event as well.

local tool = script.Parent
local equipped = false
local runService = game:GetService("RunService")

tool.Equipped:Connect(function(mouse)
	equipped = true
  while runService.Stepped:Wait() and equipped do
		if mouse and mouse.Target then
			print(mouse.Target)
    end
  end
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

@incapaxx You don’t need to use :GetMouse()

1 Like

Probably not but having the mouse accessible to other scopes would be better. The loop is infinitely running and referencing an old mouse, where as :GetMouse returns the same mouse.

Yeah, I agree it would be better to not have a while loop, because that’ll run even after you unequip it.

But if you’re only using the mouse when it’s equipped (as you should) then this is fine and works great doing what he needs.

What I sent will also only run the loop when it’s equipped rather than all the time.

I mean, the event is there for a reason. Why not make use of it. And instead of having the loop constantly function, just have it run while the tool is equipped, as it should be.

Just get the mouse from the .Equipped function and use that in the UIS.InputChanged

Edit: There’s no need to use a function to get the mouse when .Equipped gives it to you.

1 Like

Mixing my code and @incapaxx’s

local tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local con

tool.Equipped:Connect(function(cursor)
    con = UserInputService.InputChanged:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseMovement then
            print(cursor.Target)
        end
    end)
end)

tool.Unequipped:Connect(function()
  con:Disconnect()
end)

Also your code before incap would disconnect and stop functioning after it got any input that wasn’t the mouse moving.