How to undo this?

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character.HumanoidRootPart

local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

How do you undo that?

for example: I only want that effect when a tool is equipped, but how do I undo that when the tool becomes unequipped?

1 Like

Something like this:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character.HumanoidRootPart

local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local connection

connection = RunService.RenderStepped:Connect(function() --connect
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

connection:Disconnect() --disconnect the function
2 Likes

After disconnecting the function, how would I reconnect it?

For example I want said script to run while the player is playing a certain animation, but if a different animation is playing I disconnect the function, but afterwards lets say the player plays the first animation, how would I reconnect the function to run said script again?

1 Like

Run it through a function to reconnect it:

local function Reconnect()
	connection = RunService.RenderStepped:Connect(function() --connect
		local RootPos, MousePos = Root.Position, Mouse.Hit.Position
		Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
	end)
end
connection:Disconnect() --disconnect the function
Reconnect() --reconnect the function
1 Like

I get this error:

Reconnect is not a valid member of RBXScriptConnection

Run the connection again then:

connection = RunService.RenderStepped:Connect(function() --connect
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

connection:Disconnect()

--run the connection again
connection = RunService.RenderStepped:Connect(function() --connect
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)
1 Like