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)
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.
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:
Correct:
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.
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.
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