How do I stop Mouse.Move:Connect function?

I want to know how to stop a

Mouse.Move:Connect(function()
print(Mouse.Hit)
end)

After I press a certain button it starts but I don’t know how to stop it.

I looked around for a while but cannot find any source that tells me how to stop it. Can anyone help please?

You can save the function as a variable, “MouseMoveFunction” for example, then do MouseMoveFunction:Disconnect() when you’re done with it

2 Likes

Like this?

local MoveMouseFunction = Mouse.Move:Connect(function()
	torso.CFrame = CFrame.new(torso.Position, Vector3.new(Mouse.Hit.p.X, 0, Mouse.Hit.p.Z))
	print(Mouse.Hit)
    end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then 
		return
	end
if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.G  then
			Hold = true
			
			if KeyDown()then
				MoveMouseFunction:Connect()
			end
		end
	end

end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end

if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.G then
			Hold = false
			
			Mouse.Move:Disconnect()
			
			
		end
	end
end)

try something like this:

local connection

connection = randomEvent:Connect(function()

print("random event fired")

connection:Disconnect()

end)
2 Likes

faunx meant it like this:

local Variable = Mouse.Move:Connect(function()
print(Mouse.Hit)
end)

Variable:Disconnect()
1 Like

Just set it equal to a variable

local MoveConnection

MoveConnection = Mouse.Move:Connect(function()

end)

MoveConnection:Disconnect() --completely stops the functions from running
1 Like

Where it says “MoveMouseFunction:Connect()”, it still needs a function argument, like Connect normally would.
So it’d be

MoveMouseFunction = mouse.Move:Connect(function()
    torso.CFrame = CFrame.new(torso.Position, Vector3.new(Mouse.Hit.p.X, 0, Mouse.Hit.p.Z))
	print(Mouse.Hit)
end)
1 Like

So how would I end it in an if statement? because mine doesn’t seem to work when I do that.

Connecting to RBXScriptSignals/events such as workspace.ChildAdded, where you run a function each time the signal is fired, will return an RBXScriptConnection object. You can get a reference to the connection by setting it as a variable like so:

local function OnChildAdded(Instance)
    print(Instance.Name)
end

local RBXScriptConnection = workspace.ChildAdded:Connect(OnChildAdded)

If you no longer want to print out the instance’s name whenever a child is added to Workspace, then you can simply disconnect it.

RBXScriptConnection:Disconnect()

This will break the connection to the signal, so it won’t run the function anymore.
You can make sure it’s not connected with RBXScriptConnection.Connected.

2 Likes

Is there any chance you’re trying to disconnect the connection from inside of the binded function?
If so, you will need to make sure that you set a reference to it before the line the signal is on.

Incorrect:

image

Correct:

image


Why? Think of it like a variable. You cannot access var n to add 1+1, because it can’t assign what isn’t complete. If it did, it would probably cause infinite recursion.

image

2 Likes

Try this:

local mouseConn; mouseConn = Mouse.Move:Connect(function()
print(Mouse.Hit)
end)
-- Done with it
mouseConn:Disconnect()
1 Like

You could have a variadle declared outside the function that changes after the function runs. And make it so the code inside the function only runs if the variable is true.

1 Like

I realized my mistake. At first I made an if statement for when the user presses G instead of holding it.

First code:

if input.UserInputType == Enum.UserInputType.Keyboard then
		local MouseMoveFunction
		
		if input.KeyCode == Enum.KeyCode.G then
			
		if KeyDown()then
		Hold = true
		game.ReplicatedStorage.FireFist:FireServer()
				MouseMoveFunction = Mouse.Move:Connect(function()
					torso.CFrame = CFrame.new(torso.Position, Vector3.new(Mouse.Hit.p.X, 0, Mouse.Hit.p.Z))
			print(Mouse.Hit)
					if not KeyDown()then
						MouseMoveFunction:Disconnect()
					end

				end)
				
			end	
		end
	end

New code:

local MouseMoveFunction
			
		if KeyDown()then
		Hold = true
		game.ReplicatedStorage.FireFist:FireServer()
				MouseMoveFunction = Mouse.Move:Connect(function()
					torso.CFrame = CFrame.new(torso.Position, Vector3.new(Mouse.Hit.p.X, 0, Mouse.Hit.p.Z))
			print(Mouse.Hit)
					if not KeyDown()then
						MouseMoveFunction:Disconnect()
					end

				end)
				
			end

Thanks for the help everybody. Hope you guys are successful in y’all journey :+1: