Hi. I’m creating a multiplayer for my game. Finally, I got down to logic. But I’m incredibly dumb. Here my code now:
script.Parent.PlaySolo.MouseButton1Click:Connect(function()
game.ReplicatedStorage.CreateServer:FireServer()
--Server
game.ReplicatedStorage.CreateServer.OnServerEvent:Connect(function(plr)
local function generateRandomString(length)
local result = ""
for i = 1, length do
result = result .. string.char(math.random(math.random() < 0.5 and min_letter or min_number, math.random() < 0.5 and max_letter or max_number))
end
return result
end
local randomString = generateRandomString(8)
teleportService:Teleport(18444133431, game.Players.LocalPlayer, {RandomStr = randomString})
end)
end)
on Lobby place
and here on game:
local MultyplayerCode = game["Teleport Service"]:GetLocalPlayerTeleportData()
script.Parent.Text = MultyplayerCode[1] or " "
I had ideas on how to implement a server-to-server transition, but then I realized two things:
1 The lobby will restart
2 Some games will turn off and I do not know how to get it through the lobby.
And what’s the big deal? Use a datacenter! Maybe some understanding scriptwriter will answer me, but the only thing I know about TeleportService is what it has: Teleport and working with the datacenter, I just have questions about the implementation. You don’t have to write me the full code, but I don’t know. Please explain to me the train of thought and at least just start writing of the code, please.
local teleportService = game:GetService("TeleportService")
local InvokeCr = game.ReplicatedStorage.CreateServer
local InvokeJo = game.ReplicatedStorage.JoinServer
script.Parent.PlaySolo.MouseButton1Click:Connect(function()
local InvokeServerC = InvokeCr:InvokeServer()
-- here I reserve the server and throw the first player inside
teleportService:Teleport(18444133431, game.Players.LocalPlayer, {RandomStr = InvokeServerC})
end)
script.Parent.Multyplayer.TextBox.FocusLost:Connect(function(Entered,input)
if not Entered then return end
local enteredCode = input:lower()
local InvokeServerJ = InvokeJo:InvokeServer(enteredCode)
if InvokeServerJ then
-- here I send the rest of the players to an existing server
-- but how do I get a previously reserved server?
teleportService:Teleport(18444133431, game.Players.LocalPlayer, {RandomStr = InvokeServerJ})
else
script.Parent.Multyplayer.TextBox.Text = "Invalid Code"
end
end)
This is my second-game place.
Now I create this code:
-- functions and Services
game.Players.PlayerAdded:Connect(function(plr)
local Uis = plr.PlayerGui.Menu
local PlaySolo = Uis.PlaySolo
local Multiplayer = Uis.Multyplayer.TextBox
PlaySolo.MouseButton1Click:Connect(function()
local randomString = generateRandomString(8)
gameQueue:AddAsync(randomString, 1800)
local Place = teleportService:ReserveServer(18444133431)
teleportService:TeleportToPrivateServer(18444133431,Place,{plr},nil,{TeleportData = randomString})
end)
Multiplayer.FocusLost:Connect(function(Entered,input)
if not Entered then return end
local enteredCode = tostring(input):lower()
local success, result = pcall(function()
return gameQueue:ReadAsync(1)
end)
if success and result == enteredCode then
teleportService:TeleportToPrivateServer(18444133431,Place,{plr},nil,{TeleportData = result})
-- here I have issue. Server dont know what is "Place"
end
end)
end)
So what i think is after the first player teleports to the reserved server, you want other people to be able to join it. But how will they be able to join it?
Should all the servers appear in a list for the player to use from?
Or should the player type the ID of the server to join?
In my game i use both MemoryStoreService and MessagingService for this, but it depends on what you are trying to do
local MemoryStoreService = game:GetService("MemoryStoreService")
local gameQueue = MemoryStoreService:GetQueue("ActiveGameCodes", 100)
local teleportService = game:GetService("TeleportService")
local min_letter, max_letter = ("a"):byte(), ("z"):byte()
local min_number, max_number = ("0"):byte(), ("9"):byte()
local function generateRandomString(length)
local result = ""
for i = 1, length do
if math.random() < 0.5 then
result = result .. string.char(math.random(min_letter, max_letter))
else
result = result .. string.char(math.random(min_number, max_number))
end
end
print(result)
return result
end
game.Players.PlayerAdded:Connect(function(plr)
local Uis = plr.PlayerGui.Menu
local PlaySolo = Uis.PlaySolo
local Multiplayer = Uis.Multyplayer.TextBox
PlaySolo.MouseButton1Click:Connect(function()
local randomString = generateRandomString(8)
gameQueue:AddAsync(randomString, 1800)
local Place = teleportService:ReserveServer(18444133431)
teleportService:TeleportToPrivateServer(18444133431,Place,{plr},nil,{TeleportData = randomString})
end)
Multiplayer.FocusLost:Connect(function(Entered,input)
if not Entered then return end
local enteredCode = tostring(input):lower()
local success, result = pcall(function()
return gameQueue:ReadAsync(1)
end)
if success and result == enteredCode then
teleportService:TeleportToPrivateServer(18444133431,Place,{plr},nil,{TeleportData = result})
end
end)
end)
Important: TeleportData is a randomGenerated code
game code:
local MultyplayerCode = game["Teleport Service"]:GetLocalPlayerTeleportData()
script.Parent.Text = MultyplayerCode[1] or " "
Its mean in game players will see game code.
Lobby:
Game:
local MemoryStoreService = game:GetService("MemoryStoreService")
-- Use Sorted Map, not Queue because we do not need the data in order
local activeGameCodes = MemoryStoreService:GetSortedMap("ActiveGameCodes")
local teleportService = game:GetService("TeleportService")
local min_letter, max_letter = ("a"):byte(), ("z"):byte()
local min_number, max_number = ("0"):byte(), ("9"):byte()
local function generateRandomString(length)
local result = ""
for i = 1, length do
if math.random() < 0.5 then
result = result .. string.char(math.random(min_letter, max_letter))
else
result = result .. string.char(math.random(min_number, max_number))
end
end
print(result)
return result
end
game.Players.PlayerAdded:Connect(function(plr)
local Uis = plr.PlayerGui.Menu
local PlaySolo = Uis.PlaySolo
local Multiplayer = Uis.Multyplayer.TextBox
PlaySolo.MouseButton1Click:Connect(function()
local randomString = generateRandomString(8)
local serverAccessCode = teleportService:ReserveServer(18444133431)
-- Save Server Access Code to memory store so other players can use it
activeGameCodes:SetAsync(randomString, serverAccessCode, 1800)
teleportService:TeleportToPrivateServer(18444133431,serverAccessCode,{plr},nil,{TeleportData = randomString})
end)
Multiplayer.FocusLost:Connect(function(Entered,input)
if not Entered then return end
local enteredCode = tostring(input):lower()
local serverAccessCode
-- Try to get the server access code from memory store
local success, message = pcall(function()
serverAccessCode = activeGameCodes:GetAsync(enteredCode)
end)
if not success then
warn(`Could not get server access code for id {enteredCode} because :{message}`)
end
print(serverAccessCode)
if success then
teleportService:TeleportToPrivateServer(18444133431,serverAccessCode,{plr},nil,{TeleportData = serverAccessCode})
end
end)
end)
I changed the code to use Sorted Maps instead of Queues, because Queues are only needed when the data has to be in order
I had to rewrite the code a bit and add server-client interaction. It is also worth completing Pcall (success in my code is always true, which is why there are small errors with the code). I’ll fix it. The most important thing is that if you enter the correct code, everything works. Thanks!