Hello, so this code saves the location when a player leaves so the question is how do I make a script that teleports them to their last location when they're joining?
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 | end ) |
13 |
14 | game.Players.PlayerRemoving:Connect( function (plr) |
15 | DataStore:SetAsync( "last" , tostring (lastPos)) |
I added a thing to check if the player has data and if not teleports to spawn.
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.Character.HumanoidRootPart.Position = DataStore:GetAsync(plr.UserId) or Vector 3. new() --Put the spawn location position numbers in there |
10 | plr.CharacterRemoving:Connect( function (char) |
11 | lastPos = char.HumanoidRootPart.Position |
12 | end ) |
13 | end ) |
14 |
15 | game.Players.PlayerRemoving:Connect( function (plr) |
To find a player, just use game.Players.LocalPlayer
, easy as that!
You can go and play it in game and press F9 to see errors Make it a local script
Bro, ehm, i think it works due the fact you can't not use any localscript if it's not a specific player ask why, well because it works like that.