Code System Not Working

Hello, devs! I was making a reserve server system for my game and it worked! The problem now is this. How can I send the code over to the new server and allow people to join from the lobby server? I tried using DataStores to send the code over to the new server but it doesn’t seem to be working. I was thinking of using MessagingService for joining servers but I’m really confused on how to use it. Thanks for any help!

Server Script (Lobby)

local TeleportService = game:GetService("TeleportService")
local DataStoreService = game:GetService("DataStoreService")
local CodeDataStore = DataStoreService:GetGlobalDataStore()
local PlaceId = 7399821818

game.ReplicatedStorage.CreateServer.OnServerEvent:Connect(function(player)
	local code = TeleportService:ReserveServer(PlaceId)
	local succ, err = pcall(function()
		CodeDataStore:SetAsync("ReservedServer", code)
	end)
	local teleportOptions = Instance.new("TeleportOptions")
	teleportOptions:SetTeleportData(code)
	wait(5)
	TeleportService:TeleportToPrivateServer(PlaceId, code, {player})
end)

Server Script (Reserved Server)

local DataStoreService = game:GetService("DataStoreService")
local CodeDataStore = DataStoreService:GetGlobalDataStore()

game.Players.PlayerAdded:Connect(function(player)
	local code = CodeDataStore:GetAsync("ReservedServer")
	
	if type(code) == string then
		game.ReplicatedStorage.SendCode:FireClient(code)
	end
end)

Local Script (ReservedServer)

game.ReplicatedStorage.SendCode.OnClientEvent:Connect(function(player, code)
	script.Parent.TextLabel.Text = "Code: "..code
end)
1 Like

you can create a string value and parent to the player , everytime a code generated , set the code to the string value . and in other server , put a text label with a script that display the string value .

I reccomend using SubscribeAsync (go read that) and also watching alvin blox’s video

U dont need to use datastores, here is a example code @ElusiveEpix gave me on my post.

2 Likes

Pretty much, make a subscribeAsync where if u input a code it tells all servers and if the server code and the text matches, teleport player…

I tried this but I don’t really understand it. I read the article and watched the video but I can’t quite get it. What I understand is I put this in the reserved server and it’ll create a code. I want Server 1 to check if Server 2 has the code. I’m really confused. Thanks for your help so far!

I tried making the code system but it keeps failing. It just gets stuck.

Lobby Server Script

local MessagingService = game:GetService("MessagingService")
local TeleportService = game:GetService("TeleportService")
local PlaceId = 7399821818

game.ReplicatedStorage.CreateServer.OnServerEvent:Connect(function(player)
	local code = TeleportService:ReserveServer(PlaceId)
	game.ReplicatedStorage.Checkpoint1:FireClient(player)
	wait(2)
	game.ReplicatedStorage.Checkpoint2:FireClient(player)
	wait(2)
	game.ReplicatedStorage.Checkpoint3:FireClient(player)
	wait(4)
	TeleportService:TeleportToPrivateServer(PlaceId, code, {player})
	
	TeleportService.TeleportInitFailed:Connect(function()
		game.ReplicatedStorage.TeleportFailed:FireClient(player)
	end)
end)

game.ReplicatedStorage.CodeCheck.OnServerEvent:Connect(function(codentered, player)
	print(codentered)
	MessagingService:PublishAsync("GetReservedCode", codentered)
	MessagingService:SubscribeAsync("ReturnReservedCode", game.PrivateServerId)
	TeleportService:TeleportToPrivateServer(PlaceId, codentered, {player})
end)

Note - I know the code entered gets overrun by the player and that MessagingService can’t accept instances.

Reserved Server Code

local MessagingService = game:GetService("MessagingService")

-- When the server starts, generate a random code.
local function GenerateRandomString(length)
	local newStr = ""
	for _ = 1, length do
		newStr ..= string.char(math.random(65, 90)) -- A-Z; see ASCII table
	end
	return newStr
end
local ServerCode = GenerateRandomString(4)

print(ServerCode)

game.Players.PlayerAdded:Connect(function(player)
	game.ReplicatedStorage.SendCode:FireClient(player, ServerCode)
end)

MessagingService:SubscribeAsync("GetReservedCode", function(message)
	local codentered = message.Data[1] -- The code that the other server wants
	if codentered == ServerCode then
		MessagingService:PublishAsync("ReturnReservedCode", game.PrivateServerId)
	end
end)

Thanks for your help!

1 Like

Ofc you would have to modify the script…
Ok so in every server there will be 3 scripts:
RespondServer
TellServer
TeleportPlayer


TellServer will… you guessed it, tell servers that someone has sent a code… for example; a player types the code “Test” and they press ‘Send’ you would tell the TellServer script (using remote events) That they had sent a code, next u wanna send the code that the player sent (in our case “Test”) and then the script will publish that information to other servers


RespondServer is the one that will be subscribed to the Topic of your choice (if u dont get it then learn about SubscribeAsync) RespondServer will be waiting for TellServer to publish new information… in our case the code that the player send (which in our case is “Test”), the main server’s code (the server of the player who wants to join the ps) and the username of the player who wants to join the ps and now we check if Test and our private server code matches…
If the private server code is for example, “Bruh” then that doesnt match with “Test” so we dont teleport the player to the server. But if we find a private server that matches with the code then we will make another subscribeAsync and this is where TeleportPlayer comes in…


TeleportPlayer will again, listen for the topic, and if the sent code (the code that plr sent) matches with the code of the other server (ps) then RespondServer will tell TeleportPlayer that they published new information and that information is the mainServerCode and the ReservedServerId (forgot what it is) and the player that requested the code and if that mainServerCode (the server of the player who wants to join the private server) matches with the Server Code (the server’s code) we will let it pass through and using the ReservedServerId it will teleport the player that requested the code to the reservedServer.


Notes:
I forgot to put this in but using TellServer go send in the main server code (the server of the player requested to join the reserved server) and send the player’s name that wants to join the private server…
and that code that tellServer sent is mainServerCode in our case.
In respondServer send ReservedServerId, mainServerCode and the player that wants to get in the reserved server…

If i missed anything just tell me, hopefully i can help!!