So, I’m trying to teleport players back to the checkpoint which is equivalent to their leaderstat value. I’m running into a problem with the player orientation which is causing the player to constantly spawn facing one way.
player.CharacterAdded:connect(function(character)
local goto = checkpoints[checkpointStat.Value]
if goto then
repeat wait() until character.Parent
character:MoveTo(goto.Position)
else
warn("Checkpoint " .. checkpointStat.Value .. " not found")
end
end)
player.CharacterAdded:Connect(function(character)
local goto = checkpoints[checkpointStat.Value]
if goto then
character:SetPrimaryPartCFrame(goto.CFrame * CFrame.Angles(25, 0, 0))
else
warn("Checkpoint " .. checkpointStat.Value .. " not found")
end
end)
You will have to change the angles depending on which way you want the player to be facing.
Here is a script I made that has a function you can call to teleport any character to any part, and it will face the character to the forward direction the part is facing (based on the rotation around the Y axis of the part. I tested it quickly in studio and it seems to work correctly. This will ideally be in a server script:
local checkpoints = workspace.Checkpoints --folder of the checkpoint parts
function TeleportCharacterToPart(character: Model, part: BasePart)
local checkpointRotation = part.Orientation.Y
character:MoveTo(part.Position)
local characterCf = CFrame.new(character:GetPrimaryPartCFrame().Position) * CFrame.Angles(0, math.rad(checkpointRotation), 0)
character:SetPrimaryPartCFrame(characterCf)
end
game:GetService("Players").PlayerAdded:Connect(function(player)
--Teleport players when they spawn to their current checkpoint
player.CharacterAdded:Connect(function(character: Model)
character:WaitForChild("HumanoidRootPart")
task.wait()
local checkpoint = player.leaderstats.Checkpoint.Value --refers to their checkpoint value
local checkpointPart = checkpoints:FindFirstChild(checkpoint)
if checkpointPart then
TeleportCharacterToPart(character, checkpointPart) --teleport player to checkpoint
end
end)
end)
--somewhere else in code in this script whenever you want to TP them to a checkpoint:
TeleportCharacterToPart(character, somePart) --example