Can't figure out how to rotate character properly

I’m trying to implement a feature where if a player is facing a certain direction and holds their mouse button, the player’s character rotates (kinda) to the opposite way of their current direction and their rotation movement is locked, so they are unable to freely rotate their character. Unfortunately, I can’t seem to get this done correctly and am always approached with a wonky problem.

This is my code and how it looks in-game:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

local LockDirection = false

local function PlayerFacingCamera()
	local HeadAngle
	local CameraHeadOffset = math.abs(Camera.CFrame:ToEulerAnglesXYZ()-Character.HumanoidRootPart.Orientation.Y) * 100
	if CameraHeadOffset > 90 then
		return true
	end
	return false
end


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			-- Is the player's HumanoidRootPart facing toward the front of the camera?
			if PlayerFacingCamera() then
				LockDirection = true
				local Direction
				if Character.HumanoidRootPart.Orientation.Y < 90 then
					-- Orientation is 0 to 90, which is the opposite range to 180
					Direction = 180
				elseif Character.HumanoidRootPart.Orientation.Y > 90 then
					-- Orientation is 90 to 180, which is the opposite range to 0
					Direction = 0
				end
				task.spawn(function()
					RunService.RenderStepped:Connect(function()
						if LockDirection == true then
							Character.HumanoidRootPart.Orientation = Vector3.new(Character.HumanoidRootPart.Orientation.X, Direction, Character.HumanoidRootPart.Orientation.Z)
						end
					end)
				end)
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		LockDirection = false
	end
end)

https://www.xbox.com/play/media/LP5TLQ8J

I’ve looked all over the DevForum and experimented with my code countless times to no result, and I can’t find anything that’ll help me accomplish this.

I can’t tell what it’s supposed to be doing by that description. Could you elaborate or give examples?

I figured it out. Turns out, the humanoid has its own property called AutoRotate which allows you to rotate using WASD keys. Simply turning this off gives me the result I want.

1 Like