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.
01 | local d = game:GetService( "DataStoreService" ):GetDataStore( "Positions" ) |
02 |
03 | game.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 = Vector 3. new(x,y,z) |
13 | end |
14 | end |
15 | end ) |
even the script above doesnt work.
Its called datastores. Here is a tiny example of how I would save a players positiion.
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "test" ) |
02 | wait( 1 ) |
03 | print (DataStore:GetAsync( "last" )) |
04 | print (DataStore:GetAsync( "time" )) |
05 |
06 | local lastPos = Vector 3. new( 0 , 0 , 0 ) |
07 |
08 | game.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 |
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!
01 | local d = game:GetService( "DataStoreService" ):GetDataStore( "Positions" ) |
02 | local playerPos = { } |
03 | game.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 = Vector 3. new( unpack (tab)) |
12 | repeat wait() until p.Character:WaitForChild( "Humanoid" ) |
13 | p.Character:MoveTo(NewVector) |
14 | wait( 2 ) |
15 | p.Character:MoveTo(NewVector) |
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
01 | local PlayerDataStore = require(game:GetService( 'ServerScriptService' ).PlayerDataStore) |
02 | local Players = game:GetService( 'Players' ) |
03 |
04 | Players.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 ) |
12 | end ) |