I have recently been working on a horror game with a singleplayer and multiplayer mode. I have ran into a few issues as I have been trying to teleport a party to their own server where no one else can join.
I tested the game and realized that when I start a singleplayer match, it puts me in the same server as my friend, who was testing the game with me. After a player clicks the button, it is supposed to teleport him to a private server when a remote event is fired to the server.
Here is the script.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local Event = ReplicatedStorage.Singleplayer
local id = 12385310345
Event.OnServerEvent:Connect(function(plr)
TeleportService:TeleportAsync(id, {plr})
end)
How do I make it so it will teleport the player to a server where NO ONE else can join? Should I create a copy of the multiplayer place and set the server size to 1, or is there another way to do this?
local YOUR_GAME_ID = 0000000
local GameCode = TeleportService:ReserveServer(YOUR_GAME_ID)
TeleportService:TeleportToPrivateServer(
YOUR_GAME_ID, -- the id you want to teleport to
GameCode, -- the reserve server code
{plr}, -- this is the table containing the players that will be teleported
true,
{} -- this is the data that will be sent to the reserved server
-- it can be nil if you dont want to send anything
)
When using TeleportAsync it teleports to a random server, but Using Teleport:ReserveServer(PlaceId) and Teleport:TeleportToPrivateServer(“ID”) you can create a custom server that others will need a code for
Event.OnServerEvent:Connect(function(plr))
local PrivateCode=TeleportService:ReserveServer(PlaceId)
TeleportService:TeleportToPrivateServer(PlaceId,PrivateCode,{plr},SpawnLocationName,TeleportData,LoadingScreen)
end)
notice that SpawnLocationName, TeleportData, and LoadingScreen are not necessary.
and be sure to use pcall() to catch errors and to retry incase.
also I would recommend a to create a list of like 15 PrivateCodes, and so that you wouldn’t have to wait for TeleportService to get the code. and be sure to remove and put in a new code for when it has been used.
EXAMPLE
local Reserves={}
for loop=1,15 do
while true do--this will repeat until a codes has been successfully created
local err,succ=pcall(TeleportService.ReserveServer,TeleportService,PlaceId)
if err then
table.insert(Reserves,succ)
break--this will break the while loop and move on to the next PrivateCode
end
end
end
--other code
So it teleports me to a new place, but when someone else tries to play the game, it puts them in my server. Any way to make the server private once the player is teleported Besides changing the server size?
its alright as soon as a code is used immediately set it to nil so that itll know that a new code is being generated and go up into the list for a code that is not being generated.
Sorry about this, but I am not very advanced of a scripter and I am wondering how I would link these long codes being generated to the GameCode?
I have a script here:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local Event = ReplicatedStorage.Singleplayer
local id = 12385310345
local GameCode = TeleportService:ReserveServer(id)
local Reserves={}
for loop=1,15 do
while true do--this will repeat until a codes has been successfully created
local err,succ=pcall(TeleportService.ReserveServer,TeleportService,id)
if err then
print(err)
print(succ)
table.insert(Reserves,succ)
break--this will break the while loop and move on to the next PrivateCode
end
end
end
Event.OnServerEvent:Connect(function(plr)
TeleportService:TeleportToPrivateServer(id, GameCode, {plr})
end)
I have alot of experience with scripting so I can tell you what this does
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local Event = ReplicatedStorage.Singleplayer
local id = 12385310345
local Reserves={}
local TotalReserves=15
function ReserveServer()
while true do--this will repeat until a codes has been successfully created
local err,succ=pcall(TeleportService.ReserveServer,TeleportService,id)
if err then
print(err)
print(succ)
return succ
end
end
end
function Teleport(group)
local GameCode=nil
local CodeInd
for loop=1,TotalReserves do--this will look for valid gamecode
local Code=Reserves[loop]
if Code then
GameCode=Code
CodeInd=loop
break
end
end
if CodeInd then
--Set nil immediately so that it cannot be used again
Reserves[CodeInd]=nil--this will remove the code until a new Code is created and so that
task.spawn(function()
Reserves[CodeInd]=ReserveServer()--ReplaceCode
end)
end
if not GameCode then--If no gamecode was found in the loop it will made one
GameCode=ReserveServer()
end
--this will retry 5 times
local TeleportSuccess=nil
for loop=1, 5 do
local err,succ=pcall(TeleportService.TeleportToPrivateServer,id,GameCode,group)
if err then
TeleportSuccess=true
break
end
end
--warn incase the retries fail
if not TeleportSuccess then
--New String interpolation
warn(`Couldnt teleport group:{table.unpack(group)}`)
end
end
for loop=1,TotalReserves do
Reserves[loop]=ReserveServer()
end
Event.OnServerEvent:Connect(function(plr)
Teleport({plr})
end)
Reserve Server:
Creates a game Id and returns it
Teleport:
Looks for valid GameCode
if there is no valid game code it will create one
if there is a valid code it will replace it with a new one
Will attempt 5 times to Teleport the player or group inputted
if all attempts fail it will display a warning
I havent fully tested this so I depend on you to find the errors.
In this pcall function its supposed to be local err,succ=pcall(TeleportService.TeleportToPrivateServer,TeleportService,id,GameCode,group)
I forgot to put in Teleportservice as the 2nd argument. My bad