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

I’ve found the solution to this topic.

I was messing around with it myself and I have finally figured it out!

For those who want to make one and are experiencing the same issue as me, here is the code I used!

Code with everything in it listed above.

local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("GameXPData", 1)
local num = dataStore:GetAsync(game.PlaceId.."-eventkeynum")
local dataStore2 = dss:GetDataStore("PopUpsData", 1)
local lastPlaceData = dss:GetDataStore("LastLocation", 1)
local bannedPlayers = dss:GetDataStore("BannedUsers", 1)
local healthData = dss:GetDataStore("HealthData", 3)
local eventData = dss:GetDataStore("EventData", 21)
local eventHandler = require(game.ReplicatedStorage.EasterEventModule)
local notifications = require(game.ReplicatedStorage.Notifications)
game.Players.PlayerAdded:Connect(function(plr)
	local leaderboard = Instance.new("Folder", plr)
	leaderboard.Name = "leaderstats"
	local easterEventData = Instance.new("Folder", plr)
	easterEventData.Name = "EasterEvent"
	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 isBanned = Instance.new("BoolValue", plr)
	isBanned.Name = "IsBanned"
	isBanned.Value = false

	local timeBanned = Instance.new("NumberValue", plr)
	timeBanned.Name = "TimeBanned"
	timeBanned.Value = 0

	local banReason = Instance.new("StringValue", plr)
	banReason.Name = "BanReason"
	banReason.Value = "N/A"

	local health = Instance.new("NumberValue", plr)
	health.Value = 100
	health.Name = "Health"

	local hunger = Instance.new("NumberValue", plr)
	hunger.Value = 100
	hunger.Name = "Hunger"

	local hasJoined = Instance.new("BoolValue", plr)
	hasJoined.Value = true
	hasJoined.Name = "HasJoined"

	local eggs = Instance.new("NumberValue", easterEventData)
	eggs.Value = 0
	eggs.Name = "Eggs"

	local hasEgg1 = Instance.new("BoolValue", easterEventData)
	hasEgg1.Value = false
	hasEgg1.Name = "HasEgg1"

	local hasEgg2 = Instance.new("BoolValue", easterEventData)
	hasEgg2.Value = false
	hasEgg2.Name = "HasEgg2"

	local hasEgg3 = Instance.new("BoolValue", easterEventData)
	hasEgg3.Value = false
	hasEgg3.Name = "HasEgg3"

	local hasEgg4 = Instance.new("BoolValue", easterEventData)
	hasEgg4.Value = false
	hasEgg4.Name = "HasEgg4"

	local hasEgg5 = Instance.new("BoolValue", easterEventData)
	hasEgg5.Value = false
	hasEgg5.Name = "HasEgg5"

	local hasEgg6 = Instance.new("BoolValue", easterEventData)
	hasEgg6.Value = false
	hasEgg6.Name = "HasEgg6"

	local hasEgg7 = Instance.new("BoolValue", easterEventData)
	hasEgg7.Value = false
	hasEgg7.Name = "HasEgg7"

	local hasEgg8 = Instance.new("BoolValue", easterEventData)
	hasEgg8.Value = false
	hasEgg8.Name = "HasEgg8"

	local hasEgg9 = Instance.new("BoolValue", easterEventData)
	hasEgg9.Value = false
	hasEgg9.Name = "HasEgg9"

	local hasEgg10 = Instance.new("BoolValue", easterEventData)
	hasEgg10.Value = false
	hasEgg10.Name = "HasEgg10"

	local hasEgg11 = Instance.new("BoolValue", easterEventData)
	hasEgg11.Value = false
	hasEgg11.Name = "HasEgg11"

	local hasEgg12 = Instance.new("BoolValue", easterEventData)
	hasEgg12.Value = false
	hasEgg12.Name = "HasEgg12"

	local hasEgg13 = Instance.new("BoolValue", easterEventData)
	hasEgg13.Value = false
	hasEgg13.Name = "HasEgg13"

	local hasEgg14 = Instance.new("BoolValue", easterEventData)
	hasEgg14.Value = false
	hasEgg14.Name = "HasEgg14"

	local hasEgg15 = Instance.new("BoolValue", easterEventData)
	hasEgg15.Value = false
	hasEgg15.Name = "HasEgg15"
	
	local timesrepeated = 0
	repeat
		timesrepeated += 1
		local hasBasket = Instance.new("BoolValue", easterEventData)
		hasBasket.Name = "HasBasket"..timesrepeated
		hasBasket.Value = false
	until timesrepeated == 5
	timesrepeated = 0
	repeat
		timesrepeated += 1
		local hasChocolate = Instance.new("BoolValue", easterEventData)
		hasChocolate.Name = "HasChocolate"..timesrepeated
		hasChocolate.Value = false
	until timesrepeated == 10
	local baskets = Instance.new("NumberValue", easterEventData)
	baskets.Value = 0
	baskets.Name = "Baskets"
	local CompletedObby = Instance.new("BoolValue", easterEventData)
	CompletedObby.Value = false
	CompletedObby.Name = "CompletedObby"
	local hasAnnounced = Instance.new("BoolValue", easterEventData)
	hasAnnounced.Value = false
	hasAnnounced.Name = "HasAnnounced1"
	local hasAnnounced2 = Instance.new("BoolValue", easterEventData)
	hasAnnounced2.Value = false
	hasAnnounced2.Name = "HasAnnounced2"
	local hasAnnounced3 = Instance.new("BoolValue", easterEventData)

	local data4



	for i,v in pairs(plr:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("BoolValue") or v:IsA("StringValue") or v:IsA("IntValue") then
			local data
			local success, fail = pcall(function()
				data = dataStore:GetAsync(plr.UserId..v.Name)
			end)
			if success then
				print("Successfully loaded "..plr.Name.."'s "..v.Name.."'s data!")
				v.Value = data
			end
		end
	end
	for i,v in pairs(plr.leaderstats:GetChildren()) do
		local data
		local success, fail = pcall(function()
			data = dataStore:GetAsync(plr.UserId..v.Name)
		end)
		if success then
			print("Successfully loaded "..plr.Name.."'s "..v.Name.."'s Data!")
			v.Value = data
		end
	end
	for i,v in pairs(easterEventData:GetChildren()) do
		local data
		local success, fail = pcall(function()
			data = eventData:GetAsync(plr.UserId..v.Name)
		end)
		if success then
			print("Successfully loaded "..plr.Name.."'s "..v.Name.."'s Data!")
			v.Value = data
		end
	end

	if CompletedObby.Value == false then
		eventHandler:SendNotification(plr, "You haven't completed the Easter Obby event today! You can complete it now. It is located outside right next to the farm!")
	end

	if eggs.Value < 15 then
		eventHandler:SendNotification(plr, "You haven't found all the hidden Easter Eggs! You can still find "..15-eggs.Value.." more hidden Easter Eggs! Good luck :D")
	end
	
	local arrivaldata = plr:GetJoinData()
	local tpdata = arrivaldata.TeleportData
	--[[if tpdata["CFrame"] ~= nil then
		wait(4)
		local success, fail = pcall(function()
			plr.Character:WaitForChild("HumanoidRootPart").CFrame = tpdata["CFrame"]
		end)
		if success then
			print("Brought player to last spot")
		else
			wait(1)
			notifications:SendNotification(plr, "Oh-no! \n\n We ran into an error when bringing you to where you last were. Error: "..fail..".", "Error!")
			warn("Failed to bring player to last spot "..fail)
		end
	end]]
	local data4
	local success, fail = pcall(function()
		data4 = lastPlaceData:GetAsync(plr.UserId.."-lastposition")
	end)

			--[[local position = Instance.new("CFrameValue", plr)
			position.Value.Position.X = Vector3.new(data4["x"])
			position.Value.Position.Y = data4["y"]
			position.Value.Position.Z = data4["z"] ]]

	if data4 ~= "" or success then
		wait(4)

		plr.Character.HumanoidRootPart.Position = Vector3.new(lastPlaceData:GetAsync(plr.UserId.."-lastpositionx"), lastPlaceData:GetAsync(plr.UserId.."-lastpositiony"), lastPlaceData:GetAsync(plr.UserId.."-lastpositionz"))
		wait(0.5)
		lastPlaceData:RemoveAsync(plr.UserId.."-lastpositionx")
		lastPlaceData:RemoveAsync(plr.UserId.."-lastpositiony")
		lastPlaceData:RemoveAsync(plr.UserId.."-lastpositionz")
		print("Successfully loaded last position data.")
	elseif fail then
		warn("Failed to load last position data for "..plr.Name.."! Fail: "..fail)
	end


	plr.CharacterAdded:Connect(function(char)
		plr.WorkArea.Value = "None"
	end)


	if isBanned.Value == true then
		local suceed,failed = pcall(function()
			game:GetService("TeleportService"):Teleport(12457693715, plr)
		end)

		if suceed then
			print("Successfully teleported "..plr.Name.." to the restricted area!")
		else
			plr:Kick("[Auto Kick]: You are banned form King's Kingdom! Reason: Restricted area teleport failed or timed out.")
		end
	end
	local HttpService = game:GetService("HttpService")
	plr.Chatted:Connect(function(msg)

		if msg == "/rejoin" then
		--[[	local teleportService = game:GetService("TeleportService")
			local plrcount = 0
			local options = Instance.new("TeleportOptions")
print("Setting teleport data")
		--	notifications:SendNotification(plr,"❌Uh-oh!❌ \n\n Sorry, but this feature is temporarily disabled due to bugs with the event. This feature will be enabled again after the Easter Event is over.","Error!")
			for i,v in pairs(game.Players:GetPlayers()) do
				i += 1
				plrcount = i
			end
			if plrcount >= 3 then
				local positionTable = {["CFrame"] = plr.Character.HumanoidRootPart.CFrame} --["x"] = plr.Character.HumanoidRootPart.Position.X, ["y"] = plr.Character.HumanoidRootPart.Position.Y, ["z"] = plr.Character.HumanoidRootPart.Position.Z}
				options:SetTeleportData(positionTable)
				teleportService:Teleport(12073023350, plr, options	)
			else
				local positionTable = {["CFrame"] = plr.Character.HumanoidRootPart.CFrame} --["x"] = plr.Character.HumanoidRootPart.Position.X, ["y"] = plr.Character.HumanoidRootPart.Position.Y, ["z"] = plr.Character.HumanoidRootPart.Position.Z}
				options:SetTeleportData(positionTable)
				teleportService:Teleport(13206085683, plr, positionTable)
			end]]
			local PositionTable = HttpService:JSONEncode({["x"] = plr.Character:WaitForChild("HumanoidRootPart").Position.X,["y"] = plr.Character:WaitForChild("HumanoidRootPart").Position.Y,["z"] = plr.Character:WaitForChild("HumanoidRootPart").Position.Z})
			lastPlaceData:SetAsync(plr.UserId.."-lastpositionx", plr.Character.HumanoidRootPart.Position.X)
			lastPlaceData:SetAsync(plr.UserId.."-lastpositiony", plr.Character.HumanoidRootPart.Position.Y)
			lastPlaceData:SetAsync(plr.UserId.."-lastpositionz", plr.Character.HumanoidRootPart.Position.Z)
			
			wait(0.5)
			local plrcount
			for i,v in pairs(game.Players:GetPlayers()) do
				i += 1
				plrcount = i
			end
			if plrcount >= 3 then
				game:GetService("TeleportService"):Teleport(12073023350, plr)
			else
				game:GetService("TeleportService"):Teleport(13206085683, plr)
			end
		end
	end)

	plr.Chatted:Connect(function(msg)
		if plr:GetRankInGroup(7048567) >= 20 then
			if msg == "-reseteggs" then
				num += 1
	--[[]		local success, fail = pcall(function()
					dataStore:SetAsync(game.PlaceId.."-eventkeynum", datanum)
				end)
				if success then]]
				local success, fail = pcall(function()
					eventData:SetAsync(game.PlaceId.."-egglocation", math.random(1,2))
				end)
				if success then
				notifications:SendNotification(plr,"✅Success! \n\n You have successfully reset the Easter Event data. The servers will shut down shortly.", "Hey!")
				notifications:SendGlobalChatNotification("[System]: "..plr.Name.." has reset the Easter Event data. The servers will shut down shortly.")

				wait(5)
				for i,v in pairs(game.Players:GetPlayers()) do
					local ui = script.Shutdown:Clone()
					ui.Parent = v.PlayerGui
				end
				wait(10)
				for i,v in pairs(game.Players:GetPlayers()) do
					local success, fail = pcall(function()
						game:GetService("TeleportService"):Teleport(12305853026, v)
					end)
					if success then
						print("Teleporting user...")
					else
						v:Kick("[Auto Kick]: Your teleport failed. Error: "..fail..". Please screen shot this and open a ticket on our communications server so the developers can fix this bug.")
					end
				end
				--	else
				--	notifications:SendNotification("Error!", "❌Uh=oh!❌\n\n Sorry, but there was an error when attempting to perform the action of resetting the egg hunt. Error: "..fail..". Please try again later.")
					--end
				else
					notifications:SendNotification(plr,"❌Error! \n\n There was an error when attempting to reset the egg locations. Please try again later, if this happens again please say '-globalshutdown'. Error: "..fail,"Hey!")
				end
				end
		end
	end)
	plr.Chatted:Connect(function(msg)
		if plr:GetRankInGroup(7048567) >= 20 then
			local words = string.split(msg, " ")
			if words[1] == "-ban" then
				local id = words[2]
				local userID = tonumber(id)
				local timeBanned = words[3]
				local reason = words[4]
				local reasonNum = tonumber(reason)


				local success, fail = pcall(function()
					--	dataStore:SetAsync(userID.."IsBanned", true)
					local player = game.Players:GetPlayerByUserId(userID)
					player.IsBanned.Value = true
					--	dataStore:SetAsync(userID.."TimeBanned", tonumber(timeBanned))
					player.TimeBanned.Value = tonumber(timeBanned)

					if reasonNum == 1 then
						--		dataStore:SetAsync(userID.."BanReason", "You have been fired / demoted by "..plr.Name..".")
						banReason.Value = "You have been fired / demoted by "..plr.Name.."."
					elseif reasonNum == 2 then
						--		dataStore:SetAsync(userID.."BanReason", "You have been punished for breaking a rule on our striking table and reaching the required punishment level to be jailed! Banned by: "..plr.Name..".")
						banReason.Value = "You have been punished for breaking a rule on our striking table and reaching the required punishment level to be jailed! Banned by: "..plr.Name.."."
					elseif reasonNum == 3 then
						--	dataStore:SetAsync(userID.."BanReason", "You have been purged by "..plr.Name..".")
						banReason.Value = "You have been purged by "..plr.Name.."."
					elseif reasonNum == 4 then
						--	dataStore:SetAsync(userID.."BanReason", "You have resigned by opening a ticket on our communications server. You were banned by: "..plr.Name..". (this is so the admin can reset)")
						banReason.Value = "You have resigned by opening a ticket on our communications server. You were banned by: "..plr.Name..". (this is so the admin can reset)"
					elseif reasonNum == 5 then
						--		dataStore:SetAsync(userID.."BanReason", "You have resigned without informing us. Banned by: "..plr.Name..". (this ban is longer because you did not tell us you were resigning)")
						banReason.Value = "You have resigned without informing us. Banned by: "..plr.Name..". (this ban is longer because you did not tell us you were resigning)"
					elseif reasonNum == 6 then
						--		dataStore:SetAsync(userID.."BanReason", "You have abused their powers and are being demoted. Banned by: "..plr.Name..".")
						banReason.Value = "You have abused their powers and are being demoted. Banned by: "..plr.Name.."."
					elseif reasonNum == 7 then
						--			dataStore:SetAsync(userID.."BanReason", "You have reached the maximum punishment level. Banned by: "..plr.Name..".")
						banReason.Value = "You have reached the maximum punishment level. Banned by: "..plr.Name.."."






					end
				end)
				if success then
					print("Successfully banned "..userID.."!")
					local player = game.Players:GetPlayerByUserId(userID)
					player:Kick("[Auto Kick]: You were kicked by a high rank. Reason: Banned.")
				else
					warn("Error banning "..userID.."! Reason: "..fail)
				end
			end
		end
	end)

	plr.Chatted:Connect(function(msg)
		if plr:GetRankInGroup(7048567) >= 20 then
			local words = string.split(msg, " ")
			if words[1] == "-unban" then
				local userID = words[2]
				local timeBanned = words[3]

				local success, fail = pcall(function()
					dataStore:RemoveAsync(userID.."-banned")
					dataStore:RemoveAsync(userID.."-bannedtime")
					game:GetService("MessagingService"):PublishAsync("Unban", plr.UserId)
				end)
				if success then
					print("Successfully unbanned "..userID.."!")
					local player = game.Players:GetPlayerByUserId(userID)
					player:Kick("[Auto Kick]: You were kicked by a high rank. Reason: Banned.")
				else
					warn("Error banning "..userID.."! Reason: "..fail)
				end
			end
		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)
	for i,v in pairs(plr:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("BoolValue") or v:IsA("StringValue") or v:IsA("IntValue") then
			local success, fail = pcall(function()
				dataStore:SetAsync(plr.UserId..v.Name, v.Value)
			end)
			if success then
				print("Successfully saved "..plr.Name.."'s "..v.Name.."'s Data!")
			end
		end
	end
	for i,v in pairs(plr.leaderstats:GetChildren()) do
		local success, fail = pcall(function()
			dataStore:SetAsync(plr.UserId..v.Name, v.Value)
		end)
		if success then
			print("Successfully saved "..plr.Name.."'s "..v.Name.."'s Data!")
		end
	end
	for i,v in pairs(plr.EasterEvent:GetChildren()) do
		local success, fail = pcall(function()
			eventData:SetAsync(plr.UserId..v.Name, v.Value)
		end)
		if success then
			print("Successfully saved "..plr.Name.."'s "..v.Name.."'s Data!")
		end
	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)

Code with just the rejoin function

local dss = game:GetService("DataStoreService")
local lastPlaceData = dss:GetDataStore("LastLocation", 1)
local data4
game.Players.PlayerAdded:Connect(function(plr)
	local success, fail = pcall(function()
		data4 = lastPlaceData:GetAsync(plr.UserId.."-lastposition")
	end)

	if data4 ~= "" or success then
		wait(4)

		plr.Character.HumanoidRootPart.Position = Vector3.new(lastPlaceData:GetAsync(plr.UserId.."-lastpositionx"), lastPlaceData:GetAsync(plr.UserId.."-lastpositiony"), lastPlaceData:GetAsync(plr.UserId.."-lastpositionz"))
		wait(0.5)
		lastPlaceData:RemoveAsync(plr.UserId.."-lastpositionx")
		lastPlaceData:RemoveAsync(plr.UserId.."-lastpositiony")
		lastPlaceData:RemoveAsync(plr.UserId.."-lastpositionz")
		print("Successfully loaded last position data.")
	elseif fail then
		warn("Failed to load last position data for "..plr.Name.."! Fail: "..fail)
	end

	plr.Chatted:Connect(function(msg)

		if msg == "/rejoin" then
local PositionTable = HttpService:JSONEncode({["x"] = plr.Character:WaitForChild("HumanoidRootPart").Position.X,["y"] = plr.Character:WaitForChild("HumanoidRootPart").Position.Y,["z"] = plr.Character:WaitForChild("HumanoidRootPart").Position.Z})
			lastPlaceData:SetAsync(plr.UserId.."-lastpositionx", plr.Character.HumanoidRootPart.Position.X)
			lastPlaceData:SetAsync(plr.UserId.."-lastpositiony", plr.Character.HumanoidRootPart.Position.Y)
			lastPlaceData:SetAsync(plr.UserId.."-lastpositionz", plr.Character.HumanoidRootPart.Position.Z)
			
			wait(0.5)
			local plrcount
			for i,v in pairs(game.Players:GetPlayers()) do
				i += 1
				plrcount = i
			end
			if plrcount >= 3 then
				game:GetService("TeleportService"):Teleport(12073023350, plr)
			else
				game:GetService("TeleportService"):Teleport(13206085683, plr)
			end
		end
	end)
end)

I hope this helps.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.