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?
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) 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 added a thing to check if the player has data and if not teleports to spawn.
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.Character.HumanoidRootPart.Position = DataStore:GetAsync(plr.UserId) or Vector3.new() --Put the spawn location position numbers in there plr.CharacterRemoving:Connect(function(char) lastPos = char.HumanoidRootPart.Position end) end) game.Players.PlayerRemoving:Connect(function(plr) DataStore:SetAsync("last",tostring(lastPos)) DataStore:SetAsync("time",tostring(tick())) end) game:BindToClose(function() wait(3) end)
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.