The issue happens because, when players got teleported the main place gets empty, causing that server to close, next time a player joins the main place, and touch the teleporter, it will create a new ReservedServer different from the one the first group of players got teleported.
Its an easy fix, just save the Password of the ReservedServer into a Datastore.
When main place server starts, it will run a check of the Datastore, if no server exist it will create a new ReservedServer, and save it into Datastore. Next time a new Main Place server starts it will perform the Datastore check, but, this time the Datastore has data in it, the password of the previous ReservedServer, and it will use those values to teleport the players there. So this function will only run once, and it will never save data of a different ReservedServer, unless you change it manually.
Heres a code that will perform everything I told you:
local idPlace = 000000 -- Change it to your secondary place ID
local Part = game.Workspace:WaitForChild("Part") -- The part to touch
local DSS = game:GetService("DataStoreService")
local TPS = game:GetService("TeleportService")
local Players = game:GetService("Players")
-- DATASTORE TO SAVE A RESERVED SERVER
local ReservedServerDSS = DSS:GetDataStore("ReservedServerDSS")
-- SAFE TELEPORT MODULE
local SafeTeleport = require(game:GetService("ServerScriptService"):WaitForChild("SafeTeleport"))
-- RESERVED SERVER DATA
local code
local pvSVid
-- RUN A FUNCTION TO FIND IN DATASTORE IF A SERVER ALREADY EXIST
local success, Server = pcall(function()
return ReservedServerDSS:GetAsync("Reseveds")
end)
-- IF DSS GETASYNC WORKED, CHECK THE DATA, IF NO DATA CREATE A SERVER AND SAVE IT
if success then
if Server then
warn("A Server already exist in DSS")
code = Server["ServerPassword"]
pvSVid = Server["ServerID"]
warn("Server Password:", code, "Server ID:", pvSVid)
else
warn("No Server in DSS create one")
local pass, SVid = TPS:ReserveServer(idPlace)
if pass and SVid then
warn("Server Reserved Sucessfully, proceeding to Save it in DSS")
local s, err = pcall(function()
return ReservedServerDSS:SetAsync("Reseveds", {ServerPassword = pass, ServerID = SVid})
end)
if s then
warn("Server successfuly saved")
code = pass
pvSVid = SVid
else
warn("DSS failed to save the server, do retries", err)
end
end
end
end
-- PLAYERS WHO ALREADY TRIGGERED THE TOUCH
local PlayersActivatedTouchTP = {}
Part.Touched:Connect(function(partTouched)
if partTouched.Name == "HumanoidRootPart" then
local isPlayer = Players:GetPlayerFromCharacter(partTouched.Parent)
if isPlayer then
if code and pvSVid then
if not table.find(PlayersActivatedTouchTP, isPlayer) then
table.insert(PlayersActivatedTouchTP, isPlayer)
if Players:GetPlayerByUserId(isPlayer.UserId) then
warn(isPlayer, "gonna be teleported to this Reserved Server", code, pvSVid)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerAccessCode = code
local isTPDone, TPresult = SafeTeleport(idPlace, {isPlayer}, teleportOptions)
if isTPDone then
table.remove(PlayersActivatedTouchTP, table.find(PlayersActivatedTouchTP, isPlayer))
end
warn("TP done?", isTPDone)
warn("PrivateServerId?", TPresult.PrivateServerId)
warn("ReservedServerAccessCode?", TPresult.ReservedServerAccessCode)
else
table.remove(PlayersActivatedTouchTP, table.find(PlayersActivatedTouchTP, isPlayer))
end
end
end
end
end
end)
Players.PlayerRemoving:Connect(function(isPlayer)
if table.find(PlayersActivatedTouchTP, isPlayer) then
table.remove(PlayersActivatedTouchTP, table.find(PlayersActivatedTouchTP, isPlayer))
end
end)