"This Mouse Is Not Active" Error

Hello. I have made a simple hit-scan gun if you unequipped the weapon it gives a error that says “This Mouse Is Not Active”. Any help? I’ve searched and all I’ve seen is posts from 2016 and 2017. Here is the code below.

local AK = script.Parent.Parent.HeavyAssaultRifle
local Crosshair = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("DynamicCrosshairGui")

local debounce = false

local MaxAmmo = script.Parent:WaitForChild("Values").MaxAmmo
local Ammo = MaxAmmo.Value

local IsEquipped = false
local IsReloading = false

AK.Equipped:Connect(function(Mouse) -- equipping
	Mouse.Icon = "rbxasset://textures/GunCursor.png"
	local function reload() -- reloading function
			IsReloading = true
			Mouse.Icon = "rbxasset://textures/GunWaitCursor.png"
			wait(2)
			Ammo = MaxAmmo.Value
			IsReloading = false
			Mouse.Icon = "rbxasset://textures/GunCursor.png"
		end
	AK.Activated:Connect(function(Player,FromPosition,ToPosition) -- shooting the gun / making it actually reload
		if debounce == false then
			debounce = true
		if Ammo > 0 and not IsReloading then
			Ammo = Ammo -1
				end
				if Mouse.Target.Parent:FindFirstChild("Humanoid") then -- checking if there is a humanoid and taking damage away from it
					AK.Hit:FireServer(Mouse.Target.Parent, 25)
					end
			elseif IsReloading == false then
				reload()
			end
		print(Ammo)
			debounce = false
		end)
	local ips = game:GetService("UserInputService")
	ips.InputBegan:Connect(function(input,gpe)
		if gpe then
			return
			end
		if input.KeyCode == Enum.KeyCode.R and IsReloading == false and Ammo ~= MaxAmmo.Value then
			reload()
			       end
			end)
end)

Here is a image of the error.

image

1 Like

Is your mouse faulty? Try replacing it, to me it looks more like a hardware problem than something on the code.

I am more than fully sure that there is nothing wrong with my mouse. This only happens when I equip and unequip the rifle.

Every time a player unequips and re-equips the tool, an additional activated event is going to be fired. Thus, you need to disconnect your events after using them.

For example:

local conn
Tool.Equipped:Connect(function(mouse)
	conn = Tool.Activated:Connect(function()		
		--activated code
	end)
end)

Tool.Unequipped:Connect(function()
	conn:Disconnect()
end)

You are connecting a new event every time the tool is equipped. This means that if you equip the weapon 6 times, it will run the Activated event 6 times for each time you activate the tool. However, the previous 5 connections made for the tool are using an “old” mouse variable. Not only is this going to give you an error like the one you posted, but is also bad practice and can cause memory leaks. If you don’t know what this means, it basically means that the computer has been given a load that is never unburdened, and so after a while it builds up a lot of space in the computer’s memory.

What you need to do is connect each of the listeners to a variable when the tool is equipped, and disconnect them when the tool is unequipped.

For example:

local Tool, UIS = script.Parent, game:GetService("UserInputService")
local ActivatedConnection, InputConnection

Tool.Equipped:Connect(function()
	ActivatedConnection = Tool.Activated:Connect(function()
		
	end)
	InputConnection = UIS.InputBegan:Connect(function(Input, GPE)
		if not GPE then
			
		end
	end)
end)

Tool.Unequipped:Connect(function()
	if ActivatedConnection then
		ActivatedConnection:Disconnect()
	end
	if InputConnection then
		InputConnection:Disconnect()
	end
end)

Or, the neater ‘table entry’ method:

local Tool, UIS = script.Parent, game:GetService("UserInputService")
local Connections = {}

Tool.Equipped:Connect(function()
	table.insert(Connections, Tool.Activated:Connect(function()
		
	end))
	table.insert(Connections, UIS.InputBegan:Connect(function(Input, GPE)
		if not GPE then
			
		end
	end))
end)

Tool.Unequipped:Connect(function()
	for _,Connection in pairs(Connections) do
		Connection:Disconnect()
	end
end)

I use the above method, but instead of using table.insert, I just make the table a dictionary and make the indices equal to the name of the listener being connected.

A hardware issue with your mouse will never cause an error in Roblox.

Thank you GFink. This really helped me! Much appreciated!

1 Like

Thank you this helped alot! Much appreciated.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.