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