Can i get acces code from reserved server?

Hi,
is it possible to get own acces code from reserved server?

2 Likes

Could you please provide a bit more info/insight on what you’re trying to do?

I have got self-inviting reserved servers, and i need to post its acces code, so i am asking, if i can get it directly from the server, or if i must post it to the server, when created?

Are you talking about TeleportToPlaceInstance? You can probably get the server ID by using MessagingService to ask all servers if they fit some criteria, and if they do, to broadcast a response with the server ID.

1 Like

Did you mean game.PrivateServerId?

1 Like

No, what I get on developer hub, for teleporting to private server, I need to put there its acces code.

When you use TeleportService:ReserveServer() it should return it’s access code.

Ik, but can I get it directly from reserved server?

Like inside the ReservedServer? If so, game.PrivateServerId gives you that.
If you’re meaning to get the code from another server then use MessagingService and have the ReservedServer PublishAsync it’s PrivateServerId.

1 Like

Really, id and access code is the same, ok that I didn’t understand from dev hub.

A reserved server access code and the PrivateServerId are not the same. You cannot join private servers without the reserved server access code and the PrivateServerId is only used for specifying what the id of the server is in case you need to send players to a server directly.

You must save the reserved access code via DataStores or some other location in order to keep it. For example, what you can do is create a DataStore for access codes and then save the reserved server access code as the value and the PrivateServerId as the key.

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

local ServerAccessCodes = DataStoreService:GetDataStore("ReservedServerAccessCodes")
local accessCode, serverId = TeleportService:ReserveServer()

-- To save
ServerAccessCodes:SetAsync(serverId, accessCode)

-- To retrieve the access code (can only be done from in the VIP Server)
local accessCode = game.PrivateServerId ~= "" and ServerAccessCodes:GetAsync(game.PrivateServerId)

This way, you can get the server access code from within the private server and that can be sent via MessagingService. Another server can receive the message then send the access code to select invited players or use it to teleport others.

cc @return_end1

20 Likes

Great example! It’s worth mentioning that private server could refer to a reserved server or VIP server. In both cases there will be a private server ID. However, only one will have an access code associated with it. These are generated (as you’ve correctly mentioned) when a server is reserved.

1 Like

The mention of VIP servers actually reminds me of PrivateServerOwnerId. This can also be used as an identifier if needbe to differentiate between reserved servers and VIP servers.

I don’t believe there are any cases where a reserved server access code is possible to be associated with a VIP server but in the case that it happens, the SetAsync can be guarded by checking if PrivateServerOwnerId is 0.

if game.PrivateServerOwnerId == 0 then
    ServerAccessCodes:SetAsync(serverId, accessCode)
end

This is more useful for other cases where you need to know if the server is reserved or a VIP server.

1 Like

Thx for solution, your example was quite useless for me, because I send the access code with first player as join data. But this really helped me

.