Save where person stands

I was trying to script a block where that if a player steps on it when they leave they respawn on the block. Though I was thinking to myself, wouldn’t it be better if when they load back in they spawn where they were last standing. Is it possible to do that? If so how would i make it? Thanks.

You can use a datastore to save the X,Y,Z coordinates of the players HumanoidRootPart. AKA Position.

To do this you could do the following.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerPosition = DataStoreService:GetDataStore("PlayerPosition")

Players.PlayerAdded:Connect(function(Player)
    local Spawned = false
    Player.CharacterAdded:Connect(function(Character)
        if Spawned then return end
        Spawned = true
        
        local SavedPosition = PlayerPosition:GetAsync(Player.UserId)
        if SavedPosition then
            Character.HumanoidRootPart.CFrame = CFrame.new(SavedPosition.X, SavedPosition.Y, SavedPosition.Z)
        end
    end)
end)

Players.PlayerRemoving:Connect(function(Player)
    if Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
        local Position = Player.Character.HumanoidRootPart.Position
        PlayerPosition:SetAsync(Player.UserId, {
            X = Position.X,
            Y = Position.Y,
            Z = Position.Z
        })
    end
end)
1 Like

Ah ok cool I see. I’ll try it out.

Edit: And I’m guessing this goes in ServerScriptService right? Just want to make sure.

Yeah, you should probably save a part’s name like a spawner, so if you want to move it it doesn’t screw up players.

After that the game finds said part and spawns the players above it.

“Spawner”…Location.Value or stuff like that.

1 Like

I would put it in ServerScriptService, yes. Just for efficiency.

1 Like

Sorry for not replying sooner. It doesn’t work, I tired adding in a bindtoclose but I only know how to add that in for leaderstats stuff not saving a player location…