45-Degree Rotation System

Hello!!! I’m attempting to make a system where a player’s rotation is constrained to 45-degree intervals like in Block Tales, but I’m having a bit of an issue. Could somebody please provide some insight?

This is what I have right now:

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(character)
	local rootPart = character:WaitForChild("HumanoidRootPart")
	local humanoid = character:FindFirstChildOfClass("Humanoid")

	RunService.RenderStepped:Connect(function()
		local moveDir = humanoid.MoveDirection
		if moveDir.Magnitude > 0 then
			local targetRotation = math.atan2(-moveDir.X, -moveDir.Z)
			rootPart.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(0, math.rad(math.round(math.deg(targetRotation) / 45) * 45), 0)
		end
	end)
end)

that a YXZ in CFrame.Angle
You can use CFrame.fromEulerAnglesYXZ() as a replacement for that if it makes you more comformable to format things.