Player will face where it's moving at

So I was making a sliding system but I didn’t know how to make the player face a direction. And this is what it looks like.


Script:

local uis = game:GetService("UserInputService")
local character = game.Players.LocalPlayer.Character

--credits to Juniper/EncodedLua
function cardinalConvert(dir)
	local angle = math.atan2(dir.X, -dir.Z)
	local quarterTurn = math.pi / 2
	angle = -math.round(angle / quarterTurn) * quarterTurn

	local newX = -math.sin(angle)
	local newZ = -math.cos(angle)
	if math.abs(newX) <= 1e-10 then newX = 0 end
	if math.abs(newZ) <= 1e-10 then newZ = 0 end
	return Vector3.new(newX, 0, newZ)
end

uis.InputBegan:Connect(function(key)
	--if holding down the key M then makes player slippery
	if uis:IsKeyDown(Enum.KeyCode.M) then
		for i, v in game.Players.LocalPlayer.Character:GetDescendants() do
			if v:IsA("BasePart") then
				--very slippery player like ur sliding
				v.CustomPhysicalProperties = PhysicalProperties.new(0.005, 0.3, 0.1, 1, 1)
				character.PrimaryPart.Orientation = cardinalConvert(character.Humanoid.MoveDirection)
			end
		end
	else
		for i, v in game.Players.LocalPlayer.Character:GetDescendants() do
			if v:IsA("BasePart") then
				v.CustomPhysicalProperties = PhysicalProperties.new(0.1, 0.3, 0.5, 1, 1)
			end
		end
	end
end)

Which direction are you talking about? The direction of the slide?

The direction on where the player is moving in.

local uis = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local primaryPart = character:WaitForChild("HumanoidRootPart")

local SLIPPERY_DENSITY = 0.005
local NORMAL_DENSITY = 0.1
local ROTATION_SPEED = 15

local function setSlippery(slippery)
	for i, v in ipairs(character:GetDescendants()) do
		if not v:IsA("BasePart") then continue end
		v.CustomPhysicalProperties = PhysicalProperties.new(slippery and SLIPPERY_DENSITY or NORMAL_DENSITY, 0.3, slippery and 0.1 or 0.5, 1, 1)
	end
end

local function updateOrientation(dt)
	if humanoid.MoveDirection.Magnitude > 0 then
		local targetCFrame = CFrame.lookAt(primaryPart.Position, primaryPart.Position + humanoid.MoveDirection.Unit)
		local currentCFrame = primaryPart.CFrame
		local angleDifference = currentCFrame:ToObjectSpace(targetCFrame).Rotation.Y
		local rotationDirection = math.sign(angleDifference)
		local maxRotation = math.rad(ROTATION_SPEED) * dt
		local rotationAmount = math.clamp(angleDifference, -maxRotation, maxRotation)
		primaryPart.CFrame = currentCFrame * CFrame.Angles(0, rotationAmount, 0)
	end
end

local slipperyConnection

uis.InputBegan:Connect(function(input)
	if input.KeyCode ~= Enum.KeyCode.M then return end
	setSlippery(true)
	slipperyConnection = RunService.RenderStepped:Connect(updateOrientation)
end)

uis.InputEnded:Connect(function(input)
	if input.KeyCode ~= Enum.KeyCode.M then return end
	setSlippery(false)
	
	if not slipperyConnection then return end
	slipperyConnection:Disconnect()
	slipperyConnection = nil
end)

setSlippery(false)

Try this.

So I tried it and it didn’t work. What I’m saying is that if the player bounces off walls, it will change the player’s direction every bounce.

Make use of the Humanoid’s MoveDirection property, which returns a Vector3