Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

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!

Ad
Log in to vote
2
Answered by
Mr_Unlucky 1085 Moderation Voter
4 years ago
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()

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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)
0
thanks for copying my old code. greatneil80 2647 — 4y

Answer this question