Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script that spawns the player where they were last time in the game?

Can someone help me create a script that spawns the player where they were last time in the game? If the players position is 6,2,-9, and the player rejoins. They are back at 6,2, -9. Please tell me how to make script that does that or give me the code.

local d = game:GetService("DataStoreService"):GetDataStore("Positions")

game.Players.PlayerAdded:Connect(function(p)
    if d:GetAsync(p.UserId) then
        for _, v in pairs(d:GetAsync(p.UserId)) do
			wait(5)
            local x = d:GetAsync(p.UserId)[1]
            local y = d:GetAsync(p.UserId)[2]
            local z = d:GetAsync(p.UserId)[3]

            local h = p.Character:WaitForChild("Humanoid") -- make sure char loaded
            p.Character.HumanoidRootPart.Position = Vector3.new(x,y,z)
        end
    end
end)
game.Players.PlayerRemoving:Connect(function(p)
    local hrp = p.Character.HumanoidRootPart.Position
    d:SetAsync(p.UserId,{hrp.X,hrp.Y,hrp.Z})
end)
game:BindToClose(function()
    for _, v in pairs(game.Players:GetChildren()) do
        local x = v.Character.HumanoidRootPart.Position.X
        local y = v.Character.HumanoidRootPart.Position.Y
        local z = v.Character.HumanoidRootPart.Position.Z
        d:SetAsync(v.UserId,{x,y,z})
    end
end)

even the script above doesnt work.

3 answers

✓ Accepted answer

Its called datastores. Here is a tiny example of how I would save a players positiion.

local DataStore = game:GetService("DataStoreService"):GetDataStore("test")
wait(1)
print(DataStore:GetAsync("last"))
print(DataStore:GetAsync("time"))

local lastPos = Vector3.new(0,0,0)

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterRemoving:Connect(function(char)
        lastPos = char.HumanoidRootPart.Position
    end)
	wait(3)
	local StringVector = DataStore:GetAsync("last")
	local tab = {}
	for s in string.gmatch(StringVector,"[^,]+") do
	    table.insert(tab,tonumber(s))
	end
	local NewVector = Vector3.new(unpack(tab))
	plr.Character.HumanoidRootPart.Position = NewVector
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DataStore:SetAsync("last",tostring(lastPos))
    DataStore:SetAsync("time",tostring(tick()))
end)

game:BindToClose(function()
    wait(3)
end)

I didn't test this but hopefully it should work, it should be a in a regular script in workspace, if this helps, click accept answer, thanks!

local d = game:GetService("DataStoreService"):GetDataStore("Positions")
local playerPos = {}
game.Players.PlayerAdded:Connect(function(p)
    if d:GetAsync(p.UserId) then
    local data = d:GetAsync(p.UserId)
    local StringVector = data[1]
    local tab = {}
    for s in string.gmatch(StringVector,"[^,]+") do
       table.insert(tab,tonumber(s))
    end
    local NewVector = Vector3.new(unpack(tab))
repeat wait() until p.Character:WaitForChild("Humanoid")
    p.Character:MoveTo(NewVector)
    wait(2)
    p.Character:MoveTo(NewVector)
    print(data[1])
    end 
    spawn(function()
        while true do wait()
            pcall(function()
            playerPos[p.Name] = tostring(p.Character.HumanoidRootPart.Position)
            end)
        end
    end)
end)
game.Players.PlayerRemoving:Connect(function(p)
    d:SetAsync(p.UserId,{playerPos[p.Name]})
end)
game:BindToClose(function()
    for _, v in pairs(game.Players:GetChildren()) do
        local data = tostring(v.Character.HumanoidRootPart.Position)
        d:SetAsync(v.UserId,{data})
    end
end)

dont accept this answer accept neil's since this is just his code but changed so it uses moveto()

Put this in ServerScriptService https://www.roblox.com/library/159129148/PlayerDataStore-Module Then this script

local PlayerDataStore	= require(game:GetService('ServerScriptService').PlayerDataStore)
local Players			= game:GetService('Players')

Players.PlayerAdded:connect(function(Player)
	local SaveData	= PlayerDataStore:GetSaveData(Player)
	while not Player.Character do wait() end
	Player.Character:WaitForChild('Humanoid').RootPart.CFrame = CFrame.new(unpack(SaveData:Get('Position')))
	Player.CharacterRemoving:Connect(function(Character)
		local Position = Character.Humanoid.RootPart.Position
		SaveData:Set('Position', {Position.x, Position.y, Position.z})
	end)
end)

1 comment

0

thanks for copying my old code.

This discussion is preserved as it was. New answers, comments, and votes are closed.