I have made a button, and if you click it, it makes you type the ServerID. If the ServerID is correct it joins the server. I have made a script, but when you type in the ServerID, it doesn’t join you into the server.
Script:
local ScreenGUI = script.Parent
local Players = game:GetService("Players")
local CreateBTN = ScreenGUI.ServerSelect.CreateServer
local JoinBTN = ScreenGUI.ServerSelect.JoinServer
local JoinTextBox = ScreenGUI.ServerID.TextBox
local ServerName = ScreenGUI.Server.ServerName
local ServerIDNum = ScreenGUI.Server.CreaterPanel["Server ID"].SERVERIDNumber
local ServerID = Instance.new("IntValue")
ServerID.Parent = Players.LocalPlayer
ServerID.Name = "ServerID"
ServerID.Value = 0
CreateBTN.MouseButton1Click:Connect(function()
ServerID.Value = math.random(10000, 99999) -- ServerID
ScreenGUI.ServerSelect.Visible = false
ScreenGUI.Server.Visible = true
print(ServerID.Value)
ServerIDNum.Text = ServerID.Value
ServerName.Text = Players.LocalPlayer.Name.. " 's SERVER"
end)
JoinBTN.MouseButton1Click:Connect(function()
while true do
task.wait()
local ServerIDEntered = tonumber(JoinTextBox.Text)
if ServerIDEntered == ServerID then
print("ServerIDAccepted")
ScreenGUI.ServerSelect.Visible = false
ScreenGUI.Server.Visible = true
print(ServerID.Value)
end
end
end)
That’s because this is a LocalScript. The “Server ID” that you are trying to generate will only work for the LocalPlayer.
I’m not really sure how exactly you’re implementing this, but the key idea should be that when a new server is created, the ID is sent to the server and stored as a valid server ID. When another client enters the ID, the client checks with the server if it is a valid ID or not.
You may need to utilize Remote Functions or Remote Events for this. I would recommend you to read this Doc for more information on how you can potentially implement this.
I would recommend using a remote event sending the value of the IntValue to the server, then creating a new IntValue and changing the value of the new IntValue to the Value of the old IntValue that was sent to the server. But i havent fully comprehended what you mean by that so this may just be completely wrong, srry in advance.
The serverCreator gets an IntValue with the ServerID, it is a 5 number randomised code. The serverCreator can show the ServerID to people. The people then type down the ServerID. However, when typing in the code, the player is unable to join the Server. This is because, that the ServerID is in the client side. I am not quite sure what you mean by sending the value of the intvalue to the server, but I am not quite sure how to put the Server ID (Intvalue) to the serverside.
I got this error on line 30:
14:13:56.903 Players.planeboy2021.PlayerGui.ServerGUI.ServerJoin&CreateScript:30: attempt to index nil with ‘Value’ - Client - ServerJoin&CreateScript:30
Script:
local ScreenGUI = script.Parent
local Players = game:GetService("Players")
local CreateBTN = ScreenGUI.ServerSelect.CreateServer
local JoinBTN = ScreenGUI.ServerSelect.JoinServer
local JoinTextBox = ScreenGUI.ServerID.TextBox
local CreaterPanel = ScreenGUI.Server.CreaterPanel
local ServerName = ScreenGUI.Server.ServerName
local ServerIDNum = ScreenGUI.Server.CreaterPanel["Server ID"].SERVERIDNumber
CreateBTN.MouseButton1Click:Connect(function()
local ServerID = Instance.new("IntValue")
ServerID.Parent = game:GetService("ReplicatedStorage")
ServerID.Name = Players.LocalPlayer.Name.. "ServerID"
ServerID.Value = 0
ServerID.Value = math.random(10000, 99999) -- ServerID
ScreenGUI.ServerSelect.Visible = false
ScreenGUI.Server.Visible = true
print(ServerID.Value)
ServerIDNum.Text = ServerID.Value
ServerName.Text = Players.LocalPlayer.Name.. " 's SERVER"
end)
JoinBTN.MouseButton1Click:Connect(function()
while true do
task.wait()
local ServerIDEntered = tonumber(JoinTextBox.Text)
if ServerIDEntered == ServerID.Value then -- Error line
ScreenGUI.ServerSelect.Visible = false
ScreenGUI.Server.Visible = true
CreaterPanel.Visible = false
JoinTextBox.Text = "0"
break
end
end
end)
I’m assuming you want to use IntValues to store the IDs of the servers.
A general tip for all developers is to not trust the client as much as possible, because exploiters can change contents of a LocalScript and potentially break the game if it relies heavily on receiving data from the client. This means it’s recommended to shift the part of the code where it generates the Server ID into a ServerScript to prevent exploiters from modifying their Server ID.
Instead of creating the IntValue on the client, you can just create it through a ServerScript when the Player joins, and save it in a folder. Something like:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr)
-- Create IntValue and store under a folder in ServerScriptService
local newIdValue = Instance.new("IntValue")
newIdValue.Name = plr.Name.." ServerID"
newIdValue.Value = 0
newIdValue.Parent = script.ListOfIds
end)
How it looks like from the server:
(Make sure to also script removing the value when the Player leaves!)
From there, you can now script the part where the client communicates with the server and vice versa. I would recommend you to use RemoteFunctions in this case as you can use Client - Server - Client communication when:
The client creates a new server by clicking on the “Create Server” button → The server generates the ID for the Player and saves it in the IntValue created earlier, before returning the generated server ID → The client receives the server ID and can now display it to the client!
The client attempts to join a server by entering a server ID → The server checks through the IntValues created to see if the ID is valid and returning the appropriate response to the client.
It’s a bit to digest here, but I really recommend you to read the Doc on Client - Server - Client section of the Doc I’ve sent earlier, and see if you can figure it out yourself.