Saving Player Position And Loading it when they rejoin

Greetings!

What do i want to achieve? Well the title says it all. I would like to know how to save the players position and loading it when they rejoin. I mostly don’t know how to approach this task, so i though i would ask for help.

For reference, game like Rogue Lineage by Ragoozer save your location, and when you rejoin, it loads your character from spawn to the exact location of where you were before you left.

Should i save the CFrame to a table and load it back when the player joins the game? Should i save coordinates to a DataStore value?

Thanks for helping, hope i make my question clear!

4 Likes

Well here is a way I would go making it. And yes a good idea would be saving the player’s location by using the player’s HumanoidRootPart and getting the CFrame of it, and that will give the location of the current player, and if you try teleport the player’s HumanoidRootPart it will teleport the whole player. (note that the code maybe be not even working or doing something but its just a way to make you understand maybe a little better how CFrames work and the player)

-- // VARS
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Root = Character:FindFirstChild("HumanoidRootPart")

-- // CODE
local CFrameTable = {}

local function GetPlayerCFrame()
    table.insert(CFrameTable, Player.Name.."'s Position:"..Root.CFrame)
end)
2 Likes

I think this would save the CFrame only in the existing server. To make it work in all servers you can use DataStore and save Player’s Position in a value.

RIPPER0NI made a good tutorial about saving a player’s position. You should check it out; https://www.youtube.com/watch?v=cgu9PiCzglc.

Hope this helps. :smile:

4 Likes

You can use

local PlayerPosition = { HumanoidRootPart.CFrame:GetComponents() }

to convert a CFrame to a table if you wish to save a player’s complete position (including rotation).

:GetComponents() returns a tuple (individual values), so wrap it in a table in order to tabulate the data.

To restore it, use the following:

HumanoidRootPart.CFrame = CFrame.new(unpack(PlayerPosition))

The unpack method converts an array (a table with numeric indices) back into their individual values.

8 Likes