Trying to make a rejoin ability from my main game to my second subsidiary place

Code for main game:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")

local connectionDS = DataStoreService:GetDataStore("PlayerConnections")

Players.PlayerAdded:Connect(function(player)
	-- When a player joins the main lobby, check if they were in a match recently.
	local data = connectionDS:GetAsync(player.UserId)
	if data and os.time() - data.ConnectTime <= 120 then
		-- If they were in a match less than 2 minutes ago, wait for 4 seconds and then teleport them back.
		print("Found")
		wait(4)
		TeleportService:TeleportToPlaceInstance(game.PlaceId, data.JobId, player)
	else
		print("Not found")
	end
end)

Second:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local connectionDS = DataStoreService:GetDataStore("PlayerConnections")

Players.PlayerAdded:Connect(function(player)
	-- When a player joins the match, store their UserId with the current JobId.
	local data = {
		JobId = game.JobId,
		ConnectTime = os.time()
	}
	local success, err = pcall(function()
		connectionDS:SetAsync(player.UserId, data)
	end)

	if success then
		print("Registered")
		print("Data for player " .. player.Name .. " (" .. player.UserId .. "):")
		print("JobId: " .. data.JobId)
		print("ConnectTime: " .. data.ConnectTime)
	else
		warn("Failed to register: " .. err)
	end
end)

-- Clean up entries when the server closes.
game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		connectionDS:RemoveAsync(player.UserId)
	end
end)

Everything here works as expected, except for the rejoining mechanism. It claims that it is an unauthorized teleport to the place, while I have third party teleports on, both places are public, and I have an alt that keeps the second server from closing.

I’d like to mention, the main game script IS finding the entry, it just cannot teleport the player due to it being “Unauthorized”.

As far as I know roblox datastores do not accept tables, maybe that is the issue?
Try saving the table as a string using httpServices JSONEncode and converting it to a table when loading it via httpServices JSONDecode.

1 Like
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")  -- Added

local connectionDS = DataStoreService:GetDataStore("PlayerConnections")

Players.PlayerAdded:Connect(function(player)
	-- When a player joins the match, store their UserId with the current JobId.
	local data = {
		JobId = game.JobId,
		ConnectTime = os.time()
	}
	local jsonString = HttpService:JSONEncode(data)  -- Convert the table to a JSON string

	local success, err = pcall(function()
		connectionDS:SetAsync(player.UserId, jsonString)  -- Save the JSON string
	end)

	if success then
		print("Registered")
		print("Data for player " .. player.Name .. " (" .. player.UserId .. "):")
		print("JobId: " .. data.JobId)
		print("ConnectTime: " .. data.ConnectTime)
	else
		warn("Failed to register: " .. err)
	end
end)

-- Clean up entries when the server closes.
game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		connectionDS:RemoveAsync(player.UserId)
	end
end)
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local HttpService = game:GetService("HttpService")

local connectionDS = DataStoreService:GetDataStore("PlayerConnections")

Players.PlayerAdded:Connect(function(player)
	-- When a player joins the main lobby, check if they were in a match recently.
	local jsonString = connectionDS:GetAsync(player.UserId)
	if jsonString then
		local data = HttpService:JSONDecode(jsonString)  -- Convert the JSON string back to a table
		if data and data.ConnectTime and os.time() - data.ConnectTime <= 120 then
			-- If they were in a match less than 2 minutes ago, wait for 4 seconds and then teleport them back.
			print("Found")
			wait(4)
			TeleportService:TeleportToPlaceInstance(game.PlaceId, data.JobId, player)
		else
			print("Not found")
		end
	else
		print("No data found for player: " .. player.Name)
	end
end)

still producing the same issue, im so confused, everything else works, i can see that the main game is pulling the information from the secondary place, i dont understand whats going wrong here.

Is this in the same universe or 2 different universes? If it is 2 different ones it will not work since a datastore is just shared across a universes places and not all universes.

1 Like

Same universe, I can tell it is actively checking the datastore, if I join and I had not previously left the original server, it prints “Not Found”, but when I had previously left the server, it says Found then attempts to reconnect me to that JobId.

Could you print the jobId and placeId whenever it gets loaded in please? Maybe one of the values is nil.
And could you check up via console on your 2nd account that the JobId is the same one ?

1 Like

I think I might see the issue, hold on. Ill update you

I AM SO STUPID LOL. I was using game.placeId to join the place and I was somehow not realizing that that would register the main place instead of the subsidary. Thanks a ton for helping me realize that!

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