I'm trying to make a rejoin command and it teleports you to where you last were

I am trying to make a /rejoin command where you respawn where you were when you typed the command out, but it doesn’t work!

I got the /rejoin to work but it doesn’t teleport me back.

Instead I get
image

Code:

local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("GameXPData", 1)
local dataStore2 = dss:GetDataStore("PopUpsData", 1)
local lastPlaceData = dss:GetDataStore("LastLocation", 1)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderboard = Instance.new("Folder", plr)
	leaderboard.Name = "leaderstats"
	
	local xp = Instance.new("NumberValue", leaderboard)
	xp.Name = "XP"
	xp.Value = 0
	
	local awardedXP = Instance.new("BoolValue", plr)
	awardedXP.Value = false
	awardedXP.Name = "AwardedXP"
	
	local workArea = Instance.new("StringValue", plr)
	workArea.Value = "None"
	workArea.Name = "WorkArea"
	
	local minutes = Instance.new("NumberValue", plr)
	minutes.Value = 0
	minutes.Name = "Minutes"
	
	local checkedShop = Instance.new("BoolValue", plr)
	checkedShop.Value = false
	checkedShop.Name = "CheckedShop"
	
	local checkedChannel = Instance.new("BoolValue", plr)
	checkedChannel.Value = false
	checkedChannel.Name = "CheckedChannel"
	

	
	local data
	local data2
	local data3
	local data4
	local data5
	local data6
	local success, fail = pcall(function()
		data = dataStore:GetAsync(plr.UserId.."-xp")
		data2 = dataStore2:GetAsync(plr.UserId.."-checkedshop")
		data3 = dataStore2:GetAsync(plr.UserId.."-checkedchannel")
		data4 = lastPlaceData:GetAsync(plr.UserId.."-lastplacex")
		data5 = lastPlaceData:GetAsync(plr.UserId.."-lastplacey")
		data6 = lastPlaceData:GetAsync(plr.UserId.."-lastplacez")
		
	end)
	if success then
		xp.Value = data
		checkedShop.Value = data2
		checkedChannel.Value = data3
		if data4 ~= nil then
			local position = Instance.new("CFrameValue", plr)
			position.Value.Position.X = data4
			position.Value.Position.Y = data5
			position.Value.Position.Z = data6
			wait(2)
			plr.Character.HumanoidRootPart.CFrame = position.Value.Position
			lastPlaceData:RemoveAsync(plr.UserId.."-lastplacex")
			lastPlaceData:RemoveAsync(plr.UserId.."-lastplacey")
			lastPlaceData:RemoveAsync(plr.UserId.."-lastplacez")
		else
			lastPlaceData:RemoveAsync(plr.UserId.."-lastplacex")
			lastPlaceData:RemoveAsync(plr.UserId.."-lastplacey")
			lastPlaceData:RemoveAsync(plr.UserId.."-lastplacez")
		end
	else
		warn("Failed to load data for "..plr.Name.."!")
	end
	
	plr.CharacterAdded:Connect(function(char)
		plr.WorkArea.Value = "None"
	end)
	
	plr.Chatted:Connect(function(msg)
		if msg == "/rejoin" then
			lastPlaceData:SetAsync(plr.UserId.."-lastplacex", plr.Character.HumanoidRootPart.CFrame.X)
			lastPlaceData:SetAsync(plr.UserId.."-lastplacey", plr.Character.HumanoidRootPart.CFrame.Y)
			lastPlaceData:SetAsync(plr.UserId.."-lastplacez", plr.Character.HumanoidRootPart.CFrame.Z)
			wait(0.5)
			game:GetService("TeleportService"):Teleport(12350116527, plr)
		end
	end)
	
	while wait(60*60) do
		if plr.Minutes.Value > 0 then
			plr.Minutes.Value -= 1
			end
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, fail = pcall(function()
		dataStore:SetAsync(plr.UserId.."-xp", plr.leaderstats.XP.Value)
		dataStore2:SetAsync(plr.UserId.."-checkedshop", plr.CheckedShop.Value)
		dataStore2:SetAsync(plr.UserId.."-checkedchannel", plr.CheckedChannel.Value)
	end)
	
	if success then
		print("Saved "..plr.Name.."'s data!")
	else
		warn("Failed to save "..plr.Name.."'s data!")
	end
end)

game.ReplicatedStorage.CheckDontSeeAgain.OnServerEvent:Connect(function(plr)
	plr.CheckedShop.Value = true
	
	wait(0.2)
	local success, fail = pcall(function()
		dataStore2:SetAsync(plr.UserId.."-checkedshop", plr.CheckedShop.Value)
	end)
	if success then
		print("Saved "..plr.Name.."'s data!")
	else
		warn("Failed to save "..plr.Name.."'s data!")
	end
end)

game.ReplicatedStorage.CheckedDontSeeAgainChannel.OnServerEvent:Connect(function(plr)
	plr.CheckedChannel.Value = true
	
	wait(0.2)
	local success, fail = pcall(function()
		dataStore2:SetAsync(plr.UserId.."-checkedchannel", plr.CheckedChannel.Value)
	end)
	
	if success then
		print("Saved "..plr.Name.."'s data!")
	else
		warn("Failed to save "..plr.Name.."'s data!")
	end
end)```

Any help is appreciated!

instead of making lastplacexyz just jsonencode them

How would I go about doing that?

local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("GameXPData", 1)
local dataStore2 = dss:GetDataStore("PopUpsData", 1)
local lastPlaceData = dss:GetDataStore("LastLocation", 1)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderboard = Instance.new("Folder", plr)
	leaderboard.Name = "leaderstats"
	
	local xp = Instance.new("NumberValue", leaderboard)
	xp.Name = "XP"
	xp.Value = 0
	
	local awardedXP = Instance.new("BoolValue", plr)
	awardedXP.Value = false
	awardedXP.Name = "AwardedXP"
	
	local workArea = Instance.new("StringValue", plr)
	workArea.Value = "None"
	workArea.Name = "WorkArea"
	
	local minutes = Instance.new("NumberValue", plr)
	minutes.Value = 0
	minutes.Name = "Minutes"
	
	local checkedShop = Instance.new("BoolValue", plr)
	checkedShop.Value = false
	checkedShop.Name = "CheckedShop"
	
	local checkedChannel = Instance.new("BoolValue", plr)
	checkedChannel.Value = false
	checkedChannel.Name = "CheckedChannel"
	

	
	local data
	local data2
	local data3
	local data4
	local data5
	local data6
	local success, fail = pcall(function()
		data = dataStore:GetAsync(plr.UserId.."-xp")
		data2 = dataStore2:GetAsync(plr.UserId.."-checkedshop")
		data3 = dataStore2:GetAsync(plr.UserId.."-checkedchannel")
		data4 = dataStore2:GetAsync(plr.UserId.."-lastposition")
		
	end)
	if success then
		xp.Value = data
		checkedShop.Value = data2
		checkedChannel.Value = data3
		if data4 ~= nil then
			local position = Instance.new("CFrameValue", plr)
			position.Value.Position.X = data4["x"]
			position.Value.Position.Y = data4["y"]
			position.Value.Position.Z = data4["z"]
			wait(2)
			plr.Character.HumanoidRootPart.CFrame = position.Value.Position
			lastPlaceData:RemoveAsync(plr.UserId.."-lastposition")
		else
			lastPlaceData:RemoveAsync(plr.UserId.."-lastposition")
		end
	else
		warn("Failed to load data for "..plr.Name.."!")
	end
	
	plr.CharacterAdded:Connect(function(char)
		plr.WorkArea.Value = "None"
	end)
	
	plr.Chatted:Connect(function(msg)
		if msg == "/rejoin" then
            local PositionTable = HttpService:JSONEncode({["x"] = plr.Character:WaitForChild("HumanoidRootPart").CFrame.x,["y"] = plr.Character:WaitForChild("HumanoidRootPart").CFrame.y,["z"] = plr.Character:WaitForChild("HumanoidRootPart").CFrame.z})
			lastPlaceData:SetAsync(plr.UserId.."-lastposition", PositionTable)
			wait(0.5)
			game:GetService("TeleportService"):Teleport(12350116527, plr)
		end
	end)
	
	while wait(60*60) do
		if plr.Minutes.Value > 0 then
			plr.Minutes.Value -= 1
			end
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, fail = pcall(function()
		dataStore:SetAsync(plr.UserId.."-xp", plr.leaderstats.XP.Value)
		dataStore2:SetAsync(plr.UserId.."-checkedshop", plr.CheckedShop.Value)
		dataStore2:SetAsync(plr.UserId.."-checkedchannel", plr.CheckedChannel.Value)
	end)
	
	if success then
		print("Saved "..plr.Name.."'s data!")
	else
		warn("Failed to save "..plr.Name.."'s data!")
	end
end)

game.ReplicatedStorage.CheckDontSeeAgain.OnServerEvent:Connect(function(plr)
	plr.CheckedShop.Value = true
	
	wait(0.2)
	local success, fail = pcall(function()
		dataStore2:SetAsync(plr.UserId.."-checkedshop", plr.CheckedShop.Value)
	end)
	if success then
		print("Saved "..plr.Name.."'s data!")
	else
		warn("Failed to save "..plr.Name.."'s data!")
	end
end)

game.ReplicatedStorage.CheckedDontSeeAgainChannel.OnServerEvent:Connect(function(plr)
	plr.CheckedChannel.Value = true
	
	wait(0.2)
	local success, fail = pcall(function()
		dataStore2:SetAsync(plr.UserId.."-checkedchannel", plr.CheckedChannel.Value)
	end)
	
	if success then
		print("Saved "..plr.Name.."'s data!")
	else
		warn("Failed to save "..plr.Name.."'s data!")
	end
end)```

Thank you! I will try that out now (sorry for the late response)

I get an error!


attempt to index nil with ‘JSONEncode’.

I’m not entirely sure how to do this, but here’s a tip from me: Don’t use TeleportData as exploiters can very easily modify this to teleport themselves literally anywhere.

I don’t even know how to, but thank you for telling me! I will be careful if I use it in the future. I am trying to replicate it from “Kavra’s Kingdom” as they have a /rejoin command where you spawn back. I think they used teleport data as when I join without using the command it says “Data not found” and when I rejoin or switch servers it says “Teleport data found” in console.

1 Like

No problem! It’s typically not a good idea to rely on TeleportSetting for things such as a rejoin command due to how easy it is for people to modify the data with exploits so if you were using it and looking for a value for the location to teleport them to, they could change that value.

Of course, it all depends on what you actually use the data for, but if you were to just contain XYZ coordinates in it then an exploiter can abuse this and teleport themselves anywhere by modifying that data.

TeleportOptions Docs: TeleportOptions | Roblox Creator Documentation

Apologies, I got this mixed up with the TeleportSetting option, TeleportData is actually designed for server-side usage unlike TeleportSetting.

Edit 2: If you are going to use TeleportData, make sure that it is only sent by teleporting the player on the server-side and not through the client-side.

1 Like

So? They can teleport their character wherever there want anyway.

Wouldn’t they have to figure out the name of the location value in order to be able to change it?

Not if they have a decent enough anticheat for their experience.
My point is that this would bypass any anticheat checks since it occurs as soon as they join.

1 Like

Ooooh. Could I have an example of how I would use the teleport data to transfer from my rejoining place (teleports there since there’s noone playing my game yet), then use the data brought when they joined to travel back to the main game?

TL:DR
I made seporate game inside of the universe.

When they say /rejoin they are brought there, let’s call that place Redirects.

The data from the main game, we’ll call it game is brought over to Redirects and then after about 5 seconds, then the data brought to Redirects from the main game is brought back to the main game.

I’ve never really used TeleportData but the docs have some good explanations.
Edit: This is the recommendation from the docs but I’d recommend using Player:GetJoinData() instead of TeleportService:GetLocalPlayerTeleportData().

2 Likes

TeleportData is definitely not the way to go here, as it is meant to only be used Client Sided. You would most definitely want to use Memory Store Service’s Sorted Maps read more here

The Sorted Maps allow you to set a User’s temporary data, as opposed to DataStore (which should not be used, as that service is solely meant for long term data store). After you got your values in Sync, make sure to set a time outbound (could use tick() or os.time()) and when the player goes inbound, track the amount of seconds it took for the player to get from point A to point B.

If the time is greater than a pre-assigned value, void the data, as the player most likely left and joined back later. Depending on your game, this can be an unwanted consequence. Apart from that, make sure you RemoveAsync your data once you confirm that the data has been loaded for the player coming back.

Let me know if this makes sense.

2 Likes

This is much better than I can explain in any way, thank you.

1 Like

Always here to help. Teamwork makes the dreamwork :grinning:

1 Like

I couldn’t agree more with that. :smiley:
Edit: It’s always better for everyone when there’s teamwork involved.

How would i use memory service? Sorry if I am asking a lot, I am new to certain stuff on roblox, I am new to services that aren’t market place, data store, and teleportservice.

I tried this out, and the error I was getting earlier was that HttpService wasn’t defined, so I defined it with local HttpService = game:GetService(“HTTPService”).

I tried it, it doesn’t give an error when joining the game back when the command is run, but it isn’t teleporting me to where it should.

Like there’s no error when I return to the game.