I would like to save all my servers created by the Teleport service in a list which can be updated and read by all active servers of a game. Is that possible and if so how?
How do I create a game-wide syncing data store?
1 answer
Each server has it's own unique id, which can be called with the game.JobId. Since each server is unique, and if all players leave the server it is not used again, then you can simply create the datastore info when the first player joins a new server.
You can also check if a server is a vip server using game.PrivateServerId where these servers can be reused even if a player cancels their subscription to the game.
Server Script
local DataStoreService = game:GetService("DataStoreService")
local ServerInstance = DataStoreService:GetDataStore("OpenedServers")
game:GetService("Players").PlayerAdded:Connect(function()
if #(game:GetService("Players"):GetPlayers()) == 1 then
if game.PrivateServerId ~= "" then
if game.PrivateServerOwnerId ~= 0 then
print("VIP Server")
else
print("Reserved Server")
end
else
print("Standard Server")
end
local success, result = pcall(function()
ServerInstance:GetAsync(game.JobId)
end)
if not success then
warn(result)
else
if ServerInstance:GetAsync(game.JobId) ~= nil then
print("Old Server Found")
else
print("New Server Created")
ServerInstance:SetAsync(game.JobId, game.PlaceId)
end
end
end
end)
Teleport Service has the following function to transport players
TeleportService:TeleportToPlaceInstance(PlaceId, JobId, Player)
Where the placeid is the place of the game, and the jobid is the server of that place, and the player is the player. The only drawback to this function is that you need to know the exact numbers to define the place / job ids.
Since DataStores are global across a single game, the usage of the datastore in each server will automatically sync.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
This should be a comment and not spoon feeding code.