Before I explain what I need help with, here is the context:
I have a universe/game/experience with two places. One place is the start place which you go to when you join the game. The second place is a place that you’ll be eventually teleported to from the start place.
What I need help with:
I would like to send the player to a server in place 2, but not make a new server. I want to send the place only to an existing server. If there aren’t any existing servers, then one gets made. If all existing servers are full, then nothing happens and the player doesn’t get teleported.
I don’t know how to approach this and need help coming up with a way to do it. I know about reserved servers but idk how I could use them for this.
The goal:
If people are playing place 2, then place 2 will always have 1 server, no more will get made.
Yeah, if the memory store is shared across your two places then you can use the Server list to check if a server with the PlaceId of your second place already exists and prevent teleporting if its full, or if it there is still space in the one server you can teleport them there
you may need to modify the server ‘object’ to contain PlaceId
local function RegisterSelf()
local ServerObj = {
["JobId"] = game.JobId,
["PlaceId"] = game.PlaceId, -- <-- Added PlaceId here
["Players"] = GetPlayers(),
["Runtime"] = math.round(time()),
["FPS"] = math.round(AvgServerFPS),
["Master"] = IS_MASTER,
}
ServerIndex:SetAsync(game.JobId, HttpService:JSONEncode(ServerObj), 60)
DebugPrint("Registered self as: "..HttpService:JSONEncode(ServerObj))
end
You can use the list of servers in the loop while loop near the end of the script and modify it like this:
local Place2Exists = false
for JobId, Server in pairs(GetServers()) do
DebugPrint("JobId: "..Server.JobId..", Players: "..#Server.Players..", Master: "..(Server.Master and "Y" or "N"))
if Server.PlaceId == Place2PlaceId then
if Place2Exists then Server.Shutdown() continue end -- Shutdown second server
if #Server.Players < MaxPlayers then
Place2Exists = true
end
end
if not Master and Server.Master then Master = Server end
end
but for your case you would want to loop the server list whenever you want to teleport a player, not in the main loop, so it would make sense to extract the server list from the main script into a different script using a BindableFunction (roblox.com) (just return the Servers variable)
then it would look something like:
local BindableFunc = script.BindFunc
local Servers = BindableFunc:Invoke()
local Place2Exists = false
for i = 1, #Servers do
local Server = Servers[i]
if Server.PlaceId == Place2PlaceId then
if Place2Exists then Server.Shutdown("Only one server is allowed") continue end
Place2Exists = true
-- Teleport to Server.JobId
end
end
if not Place2Exists then
-- Teleport to PlaceId
end