Is there a way to disconnect or disable a :GetMouse() function?

So I want to make a mining system using: GetMouse().

Here’s what I have:

local player = game.Players.LocalPlayer

local tool = script.Parent


local damage = 1
local mouse = player:GetMouse()






tool.Equipped:Connect(function()
   
   mouse.Button1Down:Connect(function()

   	local clicked = mouse.Target.Parent or mouse.Target


   	local children = clicked:GetChildren()
   	if clicked:IsA("Model") then

   		if  game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value)  then
   			local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true) 
   			local hum = clicked:FindFirstChild('Humanoid')
   			for i,v in pairs(children) do
   				if v:IsA("Humanoid")then
   					game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)

   				end

   			end
   		
   		end

   	end

   end)
end)


Which, to the best of my knowledge is supposed to fire an event when the mouse clicks on a target.

here’s what itProcessing: 2021-10-28 00-51-54.mkv…
does instead:Processing: 2021-10-28 00-51-54.mkv…

In the video:
The first block was mined when the tool was equipped and then the ore was collected.
The second block however was mined even though the tool was not equipped, and so was the third.

How do I fix this pls?
Is there any way I can write:

tool.UnEquipped:Connect(function()

--- something to stop the first code?
end)

…because I’ve tried everything I can.
:slightly_frowning_face: :slightly_frowning_face:

mouse = nil

Just nil the mouse. Next time its referenced it won’t work.

local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()

tool.Equipped:Connect(function()
	mouse.Button1Down:Connect(function()
		if not mouse then
			return
		end
		local clicked = mouse.Target.Parent or mouse.Target
		local children = clicked:GetChildren()
		if clicked:IsA("Model") then
			if  game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value)  then
				local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true) 
				local hum = clicked:FindFirstChild('Humanoid')
				for i,v in pairs(children) do
					if v:IsA("Humanoid")then
						game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
					end
				end
			end
		end
	end)
end)

tool.UnEquipped:Connect(function()
	mouse = nil
end)
local connection

tool.Equipped:Connect(function()
   connection = mouse.Button1Down:Connect(function()
         -- your code
   end)
end)

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

Let’s talk about connections.
Connections are like any other object. In fact, the RBXScriptSignal:Connect call returns a connection (RBXScriptConnection).

You can disconnect a connection by using RBXScriptConnection:Disconnect().

See RBXScriptConnection and RBXScriptSignal.

local Event: BindableEvent = Instance.new("BindableEvent")
local Signal: RBXScriptSignal = Event.Event
local Connection: RBXScriptConnection = Signal:Connect(print)

Event:Fire("hello, world!") -- prints "hello, world!"
Connection:Disconnect()
Event:Fire("hello, world!") -- does not print anything

(whoops, I didn’t mean to reply to you)

I tried that and it gave an error of trying to index nil with mouse

This doesn’t work because when i want to use the knife again, the script just doesn’t work.
i had to even cut it off to film the video of the knife mining

This just might work i will try in a few hours.(Once i get the motivation to open studio again)
i want to test it before marking it as a solution

I think that the easiest way to do this is just a simple debounce. Try this script:

local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
local CanMine = false

tool.Equipped:Connect(function()
    CanMine = true
	mouse.Button1Down:Connect(function()
		if not mouse then
			return
		end
        if CanMine == false then return end
		local clicked = mouse.Target.Parent or mouse.Target
		local children = clicked:GetChildren()
		if clicked:IsA("Model") then
			if  game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value)  then
				local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true) 
				local hum = clicked:FindFirstChild('Humanoid')
				for i,v in pairs(children) do
					if v:IsA("Humanoid")then
						game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
					end
				end
			end
		end
	end)
end)

tool.UnEquipped:Connect(function()
   CanMine = false
end)
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()

tool.Equipped:Connect(function()
	if not mouse then
		return
	end
	mouse.Button1Down:Connect(function()
		local clicked = mouse.Target.Parent or mouse.Target
		local children = clicked:GetChildren()
		if clicked:IsA("Model") then
			if  game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value)  then
				local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true) 
				local hum = clicked:FindFirstChild('Humanoid')
				for i,v in pairs(children) do
					if v:IsA("Humanoid")then
						game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
					end
				end
			end
		end
	end)
end)

tool.UnEquipped:Connect(function()
	mouse = nil
end)

My bad, had the mouse debounce in the wrong place, this will work.

It gives an error of:
Attempt to call a number value

What line? Any more info regarding the error? That seems unrelated to the mouse.

Sorry, it was my mistake.
but right ran i just ran the first code and it didnt work.
im going to try the second again.

It says i tried to index nil with target, which makes sense considering the fact that i call mouse.target later on.

local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()

tool.Equipped:Connect(function()
	if not mouse then
		return
	end
	mouse.Button1Down:Connect(function()
		local clicked = mouse.Target.Parent or mouse.Target
		local children = clicked:GetChildren()
		if clicked:IsA("Model") then
			if  game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value)  then
				local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true) 
				local hum = clicked:FindFirstChild('Humanoid')
				for i,v in pairs(children) do
					if v:IsA("Humanoid")then
						game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
					end
				end
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	mouse = nil
end)

UnEquipped should have been “Unequipped”.

local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
local CanMine = false

tool.Equipped:Connect(function()
    CanMine = true
end)

tool.UnEquipped:Connect(function()
   CanMine = false
end)

mouse.Button1Down:Connect(function()
        if CanMine == false then return end
		local clicked = mouse.Target.Parent or mouse.Target
		local children = clicked:GetChildren()
		if clicked:IsA("Model") then
			if  game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value)  then
				local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true) 
				local hum = clicked:FindFirstChild('Humanoid')
				for i,v in pairs(children) do
					if v:IsA("Humanoid")then
						game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
					end
				end
			end
		end
	end)

Try this script, not sure if you saw it but it should work.
If it doesn’t work, let me know if you have any errors.
This creates a debounce so the players can only mine when the tool is open.

Would the mouse.Button1Down function even work if the mouse was nil?
Is the if not mouse then line necessary?

1 Like

Oh that’s true, let me edit that real quick, thanks.

Thanks, man.
saved me a lot of effort.

There’s also a Disconnect() method too.

local player = game.Players.LocalPlayer
local tool = script.Parent
local mouse = player:GetMouse()
local MouseConnection = nil

tool.Equipped:Connect(function()
	MouseConnection = mouse.Button1Down:Connect(function()
	end)
end)

tool.Unequipped:Connect(function()
	if MouseConnection then
		MouseConnection:Disconnect()
	end
end)