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 5 years ago
Edited 5 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.

01local d = game:GetService("DataStoreService"):GetDataStore("Positions")
02 
03game.Players.PlayerAdded:Connect(function(p)
04    if d:GetAsync(p.UserId) then
05        for _, v in pairs(d:GetAsync(p.UserId)) do
06            wait(5)
07            local x = d:GetAsync(p.UserId)[1]
08            local y = d:GetAsync(p.UserId)[2]
09            local z = d:GetAsync(p.UserId)[3]
10 
11            local h = p.Character:WaitForChild("Humanoid") -- make sure char loaded
12            p.Character.HumanoidRootPart.Position = Vector3.new(x,y,z)
13        end
14    end
15end)
View all 27 lines...

even the script above doesnt work.

3 answers

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

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

01local DataStore = game:GetService("DataStoreService"):GetDataStore("test")
02wait(1)
03print(DataStore:GetAsync("last"))
04print(DataStore:GetAsync("time"))
05 
06local lastPos = Vector3.new(0,0,0)
07 
08game.Players.PlayerAdded:connect(function(plr)
09    plr.CharacterRemoving:Connect(function(char)
10        lastPos = char.HumanoidRootPart.Position
11    end)
12    wait(3)
13    local StringVector = DataStore:GetAsync("last")
14    local tab = {}
15    for s in string.gmatch(StringVector,"[^,]+") do
View all 29 lines...

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
5 years ago
01local d = game:GetService("DataStoreService"):GetDataStore("Positions")
02local playerPos = {}
03game.Players.PlayerAdded:Connect(function(p)
04    if d:GetAsync(p.UserId) then
05    local data = d:GetAsync(p.UserId)
06    local StringVector = data[1]
07    local tab = {}
08    for s in string.gmatch(StringVector,"[^,]+") do
09       table.insert(tab,tonumber(s))
10    end
11    local NewVector = Vector3.new(unpack(tab))
12repeat wait() until p.Character:WaitForChild("Humanoid")
13    p.Character:MoveTo(NewVector)
14    wait(2)
15    p.Character:MoveTo(NewVector)
View all 34 lines...

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 5 years ago
Edited 5 years ago

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

01local PlayerDataStore   = require(game:GetService('ServerScriptService').PlayerDataStore)
02local Players           = game:GetService('Players')
03 
04Players.PlayerAdded:connect(function(Player)
05    local SaveData  = PlayerDataStore:GetSaveData(Player)
06    while not Player.Character do wait() end
07    Player.Character:WaitForChild('Humanoid').RootPart.CFrame = CFrame.new(unpack(SaveData:Get('Position')))
08    Player.CharacterRemoving:Connect(function(Character)
09        local Position = Character.Humanoid.RootPart.Position
10        SaveData:Set('Position', {Position.x, Position.y, Position.z})
11    end)
12end)
0
thanks for copying my old code. greatneil80 2647 — 5y

Answer this question