Starting a reserved server session that friends can join anytime

I’m making a RP game, and I want the player to start a private server session through something like :ReserveServer() or something in a game within the universe, where ONLY friends can choose to join that private server; from any server in the lobby. They join through the lobby place to the RP place. This is a universe.

Problem is you can’t teleport to a reserved server through JobId. How would I approach this?

3 Likes

Wouldn’t it be better to just use VIP servers? Or do you want it to be free?

You will need to save the reserve server id. You can teleport any player to the same “server” at any time with the code. Only one server will ever be running for the reserved server id. When you teleport a player to a reserved server, it will put them into the running server for the id or start up a new server for the id. Reserve server ids never expire.

If you need to get the reserved server id for the server the player is in, then you can save the id for the server they’re teleporting to in the data store before you teleport them. Something like SetAsync(user.UserId, reservedServerId).

If you need the server to know its reserved server id…

If you need to link JobIds to reserved server ids, you can do the above. Once you know the reserved server id in the new server, you then know both the reserved server id and the JobId, and you can do something like SetAsync(game.JobId, reservedServerId).

Let’s say a friend wants to join another friend, who happens to be in a reserved server. The friend would want to type in the friend’s name to join him/her.

1 Like

you could always use a data store to save the reserved server code to the players user id ? there’s also the :TeleportToPlaceInstance() method if you want to teleport to a specific job id

Here’s an example (that I have not tested) that does most of what I talked about:

local userIdJoiningReservedServerIdStore = game:GetService('DataStoreService'):GetDataStore('UserIdJoiningReservedId')
local jobIdToReservedServerIdStore = game:GetService('DataStoreService'):GetDataStore('JobIdToReservedServerId')

local function getTeleportInfoForUsername(username)
	local userId = game.Players:GetUserIdFromNameAsync(username)
	if not userId then
		return false, 'User by name '..username..' does not exist. Did you type the name wrong?'
	end
	local success, errorMsg, placeId, jobId = game['Teleport Service']:GetPlayerPlaceInstanceAsync(userId)
	if not success then
		return false, errorMsg
	end
	local reservedServerId = jobIdToReservedServerIdStore:GetAsync(jobId)
	if not reservedServerId then
		return true, 'Success', {
			reservedServerId = nil,
			placeId = placeId,
			jobId = jobId
		}
	end
	return true, 'Success', {
			reservedServerId = reservedServerId,
			placeId = placeId,
			jobId = jobId
		}
end

local function teleportPlayerToReservedServer(player, reservedServerId, placeId)
	userIdJoiningReservedServerIdStore:SetAsync(tostring(player.UserId), {
		reservedServerId = reservedServerId,
		placeId = placeId,
		time = os.time()
	})
	game['Teleport Service']:TeleportToPrivateServer(placeId, reservedServerId, {player})
end

local function teleportPlayerToUsername(player, username)
	local success, errorMsg, teleportInfo = getTeleportInfoForUsername(username)
	if not success then
		return errorMsg
	end
	if teleportInfo.reservedServerId then
		teleportPlayerToReservedServer(player, teleportInfo.reservedServerId, teleportInfo.placeId)
	else
		game['Teleport Service']:TeleportToPlaceInstance(teleportInfo.placeId, teleportInfo.jobId, player)
	end
end

local function isReservedServer()
	return game.VIPServerId ~= '' and game.VIPServerOwnerId == 0
end
local myReservedServerId

local function onPlayer(player)
	if isReservedServer() and not myReservedServerId then
		local now = os.time()
		local reservedServerInfo = userIdJoiningReservedServerIdStore:GetAsync(tostring(player.UserId))
		if reservedServerInfo and now - reservedServerInfo.time < 2*60 and reservedServerInfo.placeId == game.PlaceId then
			myReservedServerId = reservedServerInfo
			jobIdToReservedServerIdStore:SetAsync(game.JobId, myReservedServerId)
		end
	end
	userIdJoiningReservedServerIdStore:RemoveAsync(tostring(player.UserId))
end

game.Players.PlayerAdded:Connect(onPlayer)
for _, player in next, game.Players:GetPlayers() do
	spawn(function()
		onPlayer(player)
	end)
end

You would be able to use teleportPlayerToUsername(player, username) to teleport player to the user with username username.

This is only an example. You should learn from this example and implement it yourself. This example skips over some very important things like error checking in order to get across what it’s doing better,

10 Likes