Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with this code please :P?

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)

4 comments

0

try this: local stats = game.Players.LocalPlayer:WaitForChild("leaderstats"). This will award the XP to the localplayer's leaderstats

0

Edit your code and use the blue Lua symbol to generate a block where you can put your code inside. This will make your code formatted so everyone can see it clearly.

0

BloxRoxe i'm going to try that now ty! chomboghai ty changed it :D

0

Tysm BloxRoxe it's working.. im so dumb that i didn't use LocalPlayer xD

4 answers

✓ Accepted answer

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!

3 comments

0

@Ap_inity it's only working in Roblox Studio and as a Script not as a LocalScript.. help please

0

Then use a script xD

0

I can't use LocalPlayer with a script

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.

1 comment

0

Ok and i did got confused

This discussion is preserved as it was. New answers, comments, and votes are closed.