Save Location

Data Store Setup

1. Make sure your game is published to Roblox

2. Click Game Settings, then Options, then Enable API Services

3. When testing in Studio leave the game using Roblox Symbol in upper left like you would do to leave a Roblox game

4. Check Output for any error messages while testing

ServerScriptService

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local RunService = game:GetService("RunService")

startLocation = Vector3.new(0, 0, 0)


local function onPlayerJoin(player)  -- Runs when players join
    local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
 
    local location = Instance.new("Vector3Value") --Sets up value for leaderstats
    location.Name = "Location"
    location.Parent = player

    local playerLocationId = "Location_" .. player.UserId  --Gets player ID
    local LocationData = playerData:GetAsync(playerLocationId)  --Checks if player has stored data
    if LocationData then  -- Data exists for this player
        print('Data')
        print(LocationData[1])
        
        location.Value = Vector3.new(LocationData[1], LocationData[2], LocationData[3])
        print(location.Value)
        player.CharacterAdded:Connect(function(chr)
            print('Chr Ready')
            local hrp = chr:WaitForChild('HumanoidRootPart')
            RunService.Stepped:wait()
            hrp.CFrame = CFrame.new(location.Value)
        end)
    else  -- Data store is working, but no current data for this player
        location.Value = startLocation
    end
end

local function onPlayerExit(player)  --Runs when players exit
 
    local function convert(vec)  --Converts Vector to Table for Storage
        print({vec.X, vec.Y, vec.Z})
        return {vec.X, vec.Y, vec.Z}
    end
 
    local success, err = pcall(function()
        local playerLocationId = "Location_" .. player.UserId
        playerData:SetAsync(playerLocationId, convert(player.Location.Value)) --Saves player data
    end)

    if not success then
        warn('Could not save data!')
    end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Script for Checkpoint Part

local part = script.Parent

local function updateLocation(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
    local player = game.Players:FindFirstChild(otherPart.Parent.Name)
    if humanoid and player then
        player.Location.Value = humanoid.Parent.HumanoidRootPart.Position
    end
end

part.Touched:Connect(updateLocation)