Problem with Body Gyro

I have this problem with a body gyro. The expected behavior is, whenever I press shift, it zooms, and the BodyGyro turns my character to face the Mouse.Hit. However, this only works the first time. After resetting the character or calling :LoadCharacter() on it, it stops working.
This is in a Local Script

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RotatePart = Character:WaitForChild("Rotation")
local FaceMouse = RotatePart:WaitForChild("FaceMouse")
local Humanoid = Character:WaitForChild("Humanoid")
local PlayerGui = Player:WaitForChild("PlayerGui")
local Mouse = Player:GetMouse()
local GameSettings = UserSettings():GetService("UserGameSettings")
local RunService = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Camera = game.Workspace.CurrentCamera
local Tool = script.Parent
local ToolGui = Tool:WaitForChild("BlasterToolGui")
local Handle = Tool.Handle
local MuzzleAttachment = Handle.FirePosition
local FireDirection = Handle.FireDirection
local CameraPosition = Tool:WaitForChild("CameraPosition")
local equipped = false

local Zooming = false

print("Made new tool")

local function FaceMouseFunction()
	if Player.Settings.AimWithMouse.Value == true then
		FaceMouse.MaxTorque = Vector3.new(5000, 5000, 5000)
		local NewCFrame = CFrame.new(Character.Rotation.Position, Mouse.Hit.p)
		Player.Character.Rotation.FaceMouse.CFrame = NewCFrame
		print("Changed body gyro, MaxTorque: " .. tostring(FaceMouse.MaxTorque) .. " Mouse.Hit: " .. tostring(Mouse.Hit.p))
	else
		FaceMouse.MaxTorque = Vector3.new(0, 0, 0)
	end
end

local function UpdateCamera()
	Camera.CFrame = CameraPosition.CFrame
end

local function LockCamera()
	UserInput.MouseBehavior = Enum.MouseBehavior.LockCenter
	Camera.CameraType = Enum.CameraType.Scriptable
	RunService:BindToRenderStep("UpdateCamera", 199, UpdateCamera)
end

local function ReleaseCamera()
	Camera.CameraType = Enum.CameraType.Custom
	UserInput.MouseBehavior = Enum.MouseBehavior.Default
	RunService:UnbindFromRenderStep("UpdateCamera")
end

local function ZoomIn()
	if equipped == true and Camera.CameraType ~= Enum.CameraType.Watch and Zooming == false then
		LockCamera()
		Camera.FieldOfView = 40
		Zooming = true
		RunService:BindToRenderStep("FaceMouse", 199, FaceMouseFunction)
	end
end

local function ZoomOut()
	if equipped == true and Camera.CameraType ~= Enum.CameraType.Watch and Zooming == true then
		ReleaseCamera()
		Camera.FieldOfView = 70
		Zooming = false
		RunService:UnbindFromRenderStep("FaceMouse")
	end
end

local function ToggleZoom(actionName, inputState, inputObject)
	if equipped == true and inputState == Enum.UserInputState.Begin then
		if Zooming == false then
			ZoomIn()
		else
			ZoomOut()
		end
	end
end

Tool.Equipped:Connect(function()
	ContextActionService:BindAction("ToggleZoom", ToggleZoom, true, Enum.KeyCode.LeftShift)
	ContextActionService:SetTitle("ToggleZoom", "Shift")
	ContextActionService:SetPosition("ToggleZoom", UDim2.new(0.5, 100, 0.5, 0))
	equipped = true
	ToolGui.Parent = PlayerGui
end)

Tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction("ToggleZoom")
	ReleaseCamera()
	equipped = false
	ToolGui.Parent = Tool
	if Camera.FieldOfView ~= 70 then
		Camera.FieldOfView = 70
	end
end)

LockCamera()
ReleaseCamera()

The function that controls the BodyGyro are:

local function FaceMouseFunction()
	if Player.Settings.AimWithMouse.Value == true then
		FaceMouse.MaxTorque = Vector3.new(5000, 5000, 5000)
		local NewCFrame = CFrame.new(Character.Rotation.Position, Mouse.Hit.p)
		Player.Character.Rotation.FaceMouse.CFrame = NewCFrame
		print("Changed body gyro, MaxTorque: " .. tostring(FaceMouse.MaxTorque) .. " Mouse.Hit: " .. tostring(Mouse.Hit.p))
	else
		FaceMouse.MaxTorque = Vector3.new(0, 0, 0)
	end
end

All the print statements work exactly as intended. The MaxTorque is correct.

Anyone have any idea how to solve this?

Just to clarify, FaceMouse is your BodyGyro, which is inside a BasePart named “Rotation”, which is inside a character?

Going to assume that it’s the case.

You’ve only assigned the FaceMouse variable at the start. You’ll have to reassign it every time the player respawns, since the old Rotation and FaceMouse are discarded upon death. You’ll be able to see this if you put a print statement inside FaceMouseFunction() to print the parent of FaceMouse after dying.

local function updateCharacter() -- call this on CharacterAdded
	Character = Player.Character or Player.CharacterAdded:Wait()
	RotatePart = Character:WaitForChild("Rotation")
	FaceMouse = RotatePart:WaitForChild("FaceMouse")
end

The local script is inside a tool, but I guess I’ll try out your function.
(It’ll take a while, I need to sweep the floor rn)

Didn’t work D:
I’m quite sure the variables are correct, because otherwise an error would be thrown, something like, “Attempted to index nil with MaxTorque”

Hi Poisonous,

Unfortunately, even if items are deleted in workspace, they can still persist in your variables. You won’t get an error when you’re reading/writing to a property.

local a = Instance.new("WedgePart")
a.Parent = workspace

a:Destroy()

a.Name = "DestroyedWedge"
print(a.Name, a.Parent)
-- will print "DestroyedWedge nil"

From your code, it seems like it’s still trying to change the CFrame of the old BodyGyro, even though it was deleted.

Also, where is your local script located?

It is located inside a tool, in StarterPack

Soooo, anyone else have any ideas?