How to fix character HRP orientation?

I am making a skateboard and I am using welds to weld the character’s HRP (HumanoidRootPart) to the board. Then I apply an animation and rotate the character’s HRP with orientation. But when the board is unequipped, I revert the HRP orientation back to how it was before. Seems fine, right? No. The orientation messes up the character, so when I move forward, backward, etc, the character faces the wrong direction.

LocalScript Code
local tool = script.Parent
local skateboard = tool:WaitForChild("Skateboard")
local player = game.Players.LocalPlayer
local usi = game:GetService("UserInputService")
local camera = game.Workspace.Camera
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local coastingPose = tool:WaitForChild("CoastingPose")
local start = tool:WaitForChild("Start")
local stop = tool:WaitForChild("Stop")
local hrp1 = character:WaitForChild("HumanoidRootPart")
local ogOrient = hrp1.CFrame.upVector

local equipped = false
local moving = false
local placed = false
local sk
local cp = humanoid:WaitForChild("Animator"):LoadAnimation(coastingPose)

function getMousePos()
	local mouseLocation2d = usi:GetMouseLocation()
	local unitray = camera:ScreenPointToRay(mouseLocation2d.X, mouseLocation2d.Y-36, 0)
	local ray = Ray.new(unitray.Origin, unitray.Direction * 200)
	local target, position = workspace:FindPartOnRay(ray, player.Character or nil)
	return position
end

tool.Equipped:Connect(function()
	equipped = true
	sk = start:InvokeServer()
	
	local rn = humanoid:WaitForChild("Animator"):GetPlayingAnimationTracks()
	for i, v in ipairs(rn) do
		v:Stop()
	end
	
	local hrp = character:WaitForChild("HumanoidRootPart")
	local x = hrp.Orientation.X
	local z = hrp.Orientation.Z

	hrp.Orientation = Vector3.new(x,90,z)
	
	cp:Play()
end)

tool.Unequipped:Connect(function()
	
	equipped = false
	placed = false
	if sk then
		stop:FireServer()
		cp:Stop()
		
		local hrp = character:WaitForChild("HumanoidRootPart")
		
		local x = hrp.Orientation.X
		local z = hrp.Orientation.Z
		hrp.Orientation = Vector3.new(x,0,z)
	end
end)

ServerScript Code
local tool = script.Parent
local start = tool:WaitForChild("Start")
local stop = tool:WaitForChild("Stop")
local skateboard = tool:WaitForChild("Skateboard")
local sk

function onServerInvoke(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	local pos = character:WaitForChild("HumanoidRootPart").Position
	sk = skateboard:Clone()
	sk.Parent = workspace
	local x = pos.X
	local y = pos.Y + 0.65
	local z = pos.Z
	sk.CFrame = CFrame.new(character.HumanoidRootPart.Position, humanoid.MoveDirection)
	sk.Orientation = Vector3.new(0,90,0)
	
	character:MoveTo(sk.Position)
	local weld = Instance.new("Weld")
	local hrp = character:WaitForChild("HumanoidRootPart")
	weld.Part0 = sk
	weld.Part1 = hrp
	weld.C0 = sk.CFrame:Inverse() - Vector3.new(0,1,0)
	weld.C1 = hrp.CFrame:Inverse()
	weld.Parent = hrp
	weld.Name = "SkWeld"
	humanoid.JumpPower = 35
	character:FindFirstChild("Animate").Disabled = true
	humanoid.WalkSpeed = 45
	
	return sk
end

function stopFunction(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	local hrp = character:WaitForChild("HumanoidRootPart")
	local weld = hrp:FindFirstChild("SkWeld")
	if weld then weld:Destroy() end
	humanoid.JumpPower = 50
	humanoid.WalkSpeed = 16
	character:FindFirstChild("Animate").Disabled = false
	
	sk:Destroy()
end

start.OnServerInvoke = onServerInvoke
stop.OnServerEvent:Connect(stopFunction)

Link to test place: Gears Test Place - Roblox

Just set the orientation back to 0,0,0, the default orientation.

tool.Unequipped:Connect(function()
	
	equipped = false
	placed = false
	if sk then
		stop:FireServer()
		cp:Stop()
		
		local hrp = character:WaitForChild("HumanoidRootPart")
		
		hrp.Orientation = Vector3.new(0,0,0)
	end
end)

I tried that, I also tried CFrame, none of it works.

Use CFrame.fromOrientation().
When adding values to orientation, use math.rad().

So what would that look like, in code?

For example:

sk.CFrame = CFrame.new(character.HumanoidRootPart.Position, humanoid.MoveDirection) * Cframe.fromOrientation(0,math.rad(90),0)

Okay, but I’m not changing the CFrame of the skateboard, I’m basically resetting the character’s HRP’s orientation.

Try hrp.CFrame = hrp.CFrame * CFrame.fromOrientation(0,0,0).

It didn’t work, I tried it with different Y values as well.

Remember to add math.rad() when changing it to other values.
hrp.CFrame = hrp.CFrame * CFrame.fromOrientation(0,math.rad(--Your Value),0)

Oh yeah, I did that. Still doesnt work.

Try hrp.CFrame = CFrame.new(hrp.Position).
Tell me if it doesn’t work.

Doesn’t work. Character still faces the wrong direction.

Do you think I should make my own input system?

Maybe?
I had encountered this when I was playing around with the scripts, but never fixed it.
Sorry about that. :slight_smile:

1 Like