When my client code finishes lerping the character to the cframe, it would send a remote event to the server so that the server would cframe the character to that cframe. But pivoting the character cframe on the server creates a glitch where the character would be upside down and glitching.
Client:
local RunService = game:GetService("RunService")
local character = script.Parent.Parent
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local movementCoroutine = nil
local function StopMovementCoroutine()
if movementCoroutine and coroutine.status(movementCoroutine) ~= "running" then
coroutine.close(movementCoroutine)
movementCoroutine = nil
end
end
local function MoveToTarget()
StopMovementCoroutine()
local WalkToPoint = character:GetAttribute("WalkToPoint")
local WalkSpeed = character:GetAttribute("WalkSpeed")
local HipHeight = character:GetAttribute("HipHeight")
local origin = HumanoidRootPart.Position
local cframe = HumanoidRootPart.CFrame
local startTime = tick()
local elapsedTime = 0
local newPosition = WalkToPoint + Vector3.new(0, HipHeight, 0)
local direction = (newPosition - origin).Unit * Vector3.new(1, 0, 1)
local lerpCframe = CFrame.new(newPosition, newPosition + direction)
local distance = (newPosition - HumanoidRootPart.Position).Magnitude
local timeToReachTarget = distance / WalkSpeed
movementCoroutine = coroutine.create(function()
repeat
local alpha = math.clamp(elapsedTime / timeToReachTarget, 0, 1)
local lerp = cframe:Lerp(lerpCframe, alpha)
character:PivotTo(lerp)
local delta = RunService.Heartbeat:Wait()
elapsedTime += delta
until elapsedTime >= timeToReachTarget
script.Parent.MoveToFinished:FireServer(lerpCframe)
end)
coroutine.resume(movementCoroutine)
end
task.spawn(function()
repeat task.wait(0.2)
MoveToTarget()
until character:GetAttribute("Died")
end)
Server:
script.MoveToFinished.OnServerEvent:Connect(function(_, cframe)
character:SetAttribute("Moving", false)
character:PivotTo(cframe)
UpdatePosition()
end)
Video: