Hello everybody.
So i am making a private server system, a pretty simple one, the player makes a server, it shows up a join code and he shares it with his friends, they type the code and join his server.
I have been trying to make it myself, and i have got good results, but there’s only one major issue.
The issue is that im using global datastore, i save up a code and then show the code to the player, but after someone creates a server, it overrides any old servers and the old servers cannot be joined anymore, I could use plr.UserId, but i don’t know how to access it. I have looked for solutions on devforum, but i still couldn’t fix my problem.
There’s also an another problem, the join code will be the same for all the players.
Here’s my code, any help would be appreciated!
Server Script:
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local DS = game:GetService("DataStoreService")
local DS2 = DS:GetGlobalDataStore()
local DS3 = DS:GetDataStore("PrivateServers")
local code = DS3:GetAsync("ReservedServer")
local ServerCode = DS3:GetAsync("ServerCodes")
game.ReplicatedStorage.PrivateServer.OnServerInvoke = function(plr, info, data)
if info == "CreateServer" then
plr.PlayerGui.Multiplayer.Create.Text = "Creating server..."
ServerCode = math.random(111111, 999999)
code = TeleportService:ReserveServer(10021781501)
DS2:SetAsync("ReservedServer", code)
DS2:SetAsync("ServerCodes", ServerCode)
plr.PlayerGui.Multiplayer.Create.Text = "Server created, joining in..."
TeleportService:TeleportToPrivateServer(10021781501, code, {plr})
elseif info == "JoinServer" then
plr.PlayerGui.Multiplayer.Join2.Text = "Finding server..."
local ServerCode = DS2:GetAsync("ServerCodes")
if data == ServerCode then
local code = DS2:GetAsync("ReservedServer")
plr.PlayerGui.Multiplayer.Join2.Text = "Server found, joining in"
TeleportService:TeleportToPrivateServer(10021781501, code, {plr})
else
plr.PlayerGui.Multiplayer.Join2.Text = "Server not found."
wait(1)
plr.PlayerGui.Multiplayer.Join2.Text = "Join the server."
end
end
end
Create Server Button Script (local script)
local event = game.ReplicatedStorage.PrivateServer
local button = script.Parent
button.MouseButton1Click:Connect(function()
event:InvokeServer("CreateServer")
end)
Join Server Button Script (local script)
local event = game.ReplicatedStorage.PrivateServer
local button = script.Parent
button.MouseButton1Click:Connect(function()
event:InvokeServer("JoinServer", tonumber(script.Parent.Parent.Code.Text))
end)
Show join code script (server script)
local DS = game:GetService("DataStoreService")
local DS2 = DS:GetGlobalDataStore()
local DS3 = DS:GetDataStore("PrivateServers")
game.ReplicatedStorage.JoinCode.OnServerEvent:Connect(function(plr)
plr.PlayerGui.joincode.TextLabel.Text = "Join Code: "..DS2:GetAsync("ServerCodes")
end)