When serverID enter, players aren't able to join servers

Hello!!

I have made two scripts, a local and server scripts and used remote functions to communicate with them. The local script is supposed to make a serverID for players to join. Then the local script recieves the remote function to check if the player has inserted the right code. Even if the player has in typed the right code, they still won’t be able to join. Furthermore when I click on the JoinServer, immediatly says this warning:
“please enter a valid ServerID”

I admit, I did use the assistant to help me with this problem, but not only it didn’t resolve the issue it made it more complicated.

If you need more information like explorer photo, or a video, you can ask me!!!

Script: Local

local BTN = script.Parent
local Rectangle = script.Parent.Parent["Rectangle 5"]
local RectanglePrivate = script.Parent.Parent.Parent.Private["Rectangle 5"]
local Clicked = false

local ReplicatedStrage = game:GetService("ReplicatedStorage")

local PublicVaue = script.Parent.Parent.Parent.Parent.Parent.PublicValue
local PrivateVaue = script.Parent.Parent.Parent.Parent.Parent.PrivateValue

PublicVaue.Value = false
PrivateVaue.Value = false

BTN.MouseButton1Click:Connect(function()
	if Clicked == false then
		
		Rectangle.BackgroundColor3 = Color3.fromRGB(12, 177, 0)
		Clicked = true
		PublicVaue.Value = true
		RectanglePrivate.BackgroundColor3 = Color3.fromRGB(35,0,0)
		PrivateVaue.Value = false
		
		
		
	else
		
		Rectangle.BackgroundColor3 = Color3.fromRGB(19, 35, 0)
		Clicked = false
		PublicVaue.Value = false
		
		

	end
end)

Server Script:

local players = game:GetService("Players")

-- Create a table to store ServerIDs and privacy settings for each player
local serverIDs = {}
local serverPrivacy = {}

game.ReplicatedStorage.ServerID.OnServerInvoke = function(player, inputText)
    -- Check if the player already has a ServerID
    if not serverIDs[player] then
        -- Create a new ServerID for the player
        local newIdValue = Instance.new("IntValue")
        newIdValue.Name = player.Name.." ServerID"
        newIdValue.Value = math.random(10000, 99999)
        newIdValue.Parent = script.ListofIds
        serverIDs[player] = newIdValue

        -- Set the server privacy (default to public)
        serverPrivacy[player] = false -- Change to true if you want default to private
    end
    -- Return the ServerID value and privacy setting
    return serverIDs[player].Value, serverPrivacy[player]
end

players.PlayerRemoving:Connect(function(plr)
    -- Remove the player's ServerID and privacy setting when they leave
    if serverIDs[plr] then
        serverIDs[plr]:Destroy()
        serverIDs[plr] = nil
        serverPrivacy[plr] = nil
    end
end)

I’m on my phone and didn’t read the full code segments, but I would recommend adding some print statements throughout to find issues with the codes. They could be different types, maybe nil at some point, find exactly what the player typed and what the options are.

The thing is that it only checks if the user has typed in the right ServerID once, which is at the start when JoinBTN is clicked. How would you make it so it keeps checking if the user has typed in the right ServerID?

You are saying the “right code”, but never explain what that is. The code segments you sent are short, and don’t tell the server anything when the button is clicked. Are you attempting to matchmake players who type in the same code? Is there supposed to be a lobby time? More information on how you want this to work is necessary.

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.

I don’t see any code concerning server joining or code entering?

So when a player crests a server, they are teleported there immediately, and other players can join over time? I’m still not seeing what the issue is? Check through all the server IDs, and print the information on the server when you do so.

The server is a ui not a place so they are kept in a lobby, until everyone had ready upped, then they get teleported to a experience place.

So you are making a party system, not a server system atm. Send the full code, or at least all of the systems that relate to this, as you currently aren’t showing much of anything that would matter.

1 Like

I am pretty sure I have found the issue. In the code below i think it checks if the player has typed the right ServerID when the joinBTN has been pressed, which of course the user hasn’t typed in the ServerID at the start, since the value would be 0.

  JoinBTN.MouseButton1Click:Connect(function()
	
    local ServerIDEntered = tonumber(JoinTextBox.Text)
    if not ServerIDEntered then
        warn("Please enter a valid ServerID")
        return
    end

I don’t know what BTN is, but as I’ve said before, you should invoke the function when they press it with what they’ve typed in.

BTN is short for button, sorry for not explaining that.

No I understand what button means lol, but you need to be doing this from a client context, and I believe if this is like a submit button, it should invoke the server with what Id is typed in

The JoinBTN is a button where it opens up a gui where you can type in the ServerID, it is not a submit button, the player would automatically join the server/lobby when the right ServerID has been typed.

What do you mean by the last part, like this?

local ServerID, isPrivate = game.ReplicatedStorage.ServerID:InvokeServer(JoinTextBox.Text)
    if ServerID and ServerIDEntered == ServerID then
        if not isPrivate then
            ScreenGUI.ServerSelect.Visible = false
            ScreenGUI.Server.Visible = true
            CreaterPanel.Visible = false
            JoinTextBox.Text = "0"
        else
            warn("Cannot join a private server")
        end
    else
        warn("Invalid ServerID or ServerID does not match")
    end

Then why would you send the client code for it? It means nothing? It’s impossible to understand what is wrong and what you want when you A) don’t give us the code and B) don’t debug it yourself(by adding print statements)

Also, you are discussing and coding as if there is a “Correct” serverID, when there will probably be multiple parties that each have their own party ID, you just need to ensure that a party exists with that ID.

Full code: local 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, isPrivate = game.ReplicatedStorage.ServerID:InvokeServer()
    if ServerID then
        print(ServerID)
        ScreenGUI.ServerSelect.Visible = false
        ScreenGUI.Server.Visible = true
        ServerIDNum.Text = tostring(ServerID)
        ServerName.Text = Players.LocalPlayer.Name.. " 's  SERVER"
    else
        warn("ServerID is nil")
    end
end)

JoinBTN.MouseButton1Click:Connect(function()
	
    local ServerIDEntered = tonumber(JoinTextBox.Text)
    if not ServerIDEntered then
        warn("Please enter a valid ServerID")
        return
    end

    local ServerID, isPrivate = game.ReplicatedStorage.ServerID:InvokeServer(JoinTextBox.Text)
    if ServerID and ServerIDEntered == ServerID then
        if not isPrivate then
            ScreenGUI.ServerSelect.Visible = false
            ScreenGUI.Server.Visible = true
            CreaterPanel.Visible = false
            JoinTextBox.Text = "0"
        else
            warn("Cannot join a private server")
        end
    else
        warn("Invalid ServerID or ServerID does not match")
    end

ServerScript:

local players = game:GetService("Players")

-- Create a table to store ServerIDs and privacy settings for each player
local serverIDs = {}
local serverPrivacy = {}

game.ReplicatedStorage.ServerID.OnServerInvoke = function(player, inputText)
    -- Check if the player already has a ServerID
    if not serverIDs[player] then
        -- Create a new ServerID for the player
        local newIdValue = Instance.new("IntValue")
        newIdValue.Name = player.Name.." ServerID"
        newIdValue.Value = math.random(10000, 99999)
        newIdValue.Parent = script.ListofIds
        serverIDs[player] = newIdValue

        -- Set the server privacy (default to public)
        serverPrivacy[player] = false -- Change to true if you want default to private
    end
    -- Return the ServerID value and privacy setting
    return serverIDs[player].Value, serverPrivacy[player]
end

players.PlayerRemoving:Connect(function(plr)
    -- Remove the player's ServerID and privacy setting when they leave
    if serverIDs[plr] then
        serverIDs[plr]:Destroy()
        serverIDs[plr] = nil
        serverPrivacy[plr] = nil
    end
end)

In here it stores all of the different ServerID’s. The folder called ListofIds

{91D134D9-80B0-4931-ADEC-7B7227FD575E}

Read this, there shouldn’t be a single “correct” ID, also, you have one remote function that you are trying to do two separate things with, I would split it up into two. One will be the Create RF, and the other will be the Join RF.

1 Like