Failed to join server when typed in the right ServerID

Hello!

A player can create a server/lobby with a unique code called ServerID. Players can enter that code to join the server/lobby. When the right code has been typed it says these two warnings:
Failed to join server with ID: planeboy2021
Failed to join the server

Another issue is that in the JoinServerHandler, at the bottom of the script, it should print “recieved” but it doesn’t

I have made three scripts, a local and server script.

The two local scripts are in charge of the ui and when the player clicks on a button. Two two scripts are called. JoinServerHandler and ServerJoinCreateScript

JoinServerHandler = detects if player has pressed the JoinServerBTN (this pops up a ui for the player to type in the ServerID) Then once the player has typed the ServerID and presses the enter key which fires a remote function. Here is where the Failed to join the server warning is located.

ServerJoinCreate Script = detects when the player has clicked the CreateServerBTN, then fires a remote function to the server. It also adds some ui changes.

ServerScript = In charge to make the ServerID when the player makes a new server, and to delete the ServerID when the player leaves the game. Here is where the Failed to join server with ID: planeboy2021 warning is located.

I have added a print statment which prints “received” The recieved never gets printed, and it is supposed to get printed.

If you have any questions ask me!! :grin:

full scripts:

local: JoinServerHandler

local UserInputService = game:GetService("UserInputService")
local JoinBTN = script.Parent.ServerSelect.JoinServer
local JoinTextBox = script.Parent.ServerID.TextBox
local players = game:GetService("Players").LocalPlayer

JoinBTN.MouseButton1Click:Connect(function()
	UserInputService.InputBegan:Connect(function(input, gameProcessed)
		if input.KeyCode == Enum.KeyCode.Return then
			print("keycode")
			local ServerID = JoinTextBox.Text
			if ServerID and ServerID ~= "" then
				local success = game.ReplicatedStorage.JoinServer:InvokeServer(players, ServerID)
				if success then
					print("Successfully joined the server.")
				else
					warn("Failed to join the server.")
				end
			else
				warn("ServerID is nil or empty")
			end
		end
	end)

end)

game.ReplicatedStorage.JoinServer.OnClientInvoke = function()
	print("recived")
end

local: ServerJoinCreateScript

local ScreenGUI = script.Parent
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

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

ScreenGUI.Server.CreaterPanel.Visible = false

CreateBTN.MouseButton1Click:Connect(function()
    local ServerID, isPrivate = game.ReplicatedStorage.CreateServer:InvokeServer()
    if ServerID then
        print(ServerID)
		ScreenGUI.ServerSelect.Visible = false
		ScreenGUI.Server.CreaterPanel.Visible = true
        ScreenGUI.Server.Visible = true
        ServerIDNum.Text = tostring(ServerID)
        ServerName.Text = Players.LocalPlayer.Name.. " 's  SERVER"
    else
        warn("ServerID is nil")
    end
end)

Server Script:

local players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

game.ReplicatedStorage.CreateServer.OnServerInvoke = function(player, code)
    local ServerID = Instance.new("IntValue")
    ServerID.Value = math.random(10000, 99999)
    ServerID.Parent = ServerScriptService.ServerIDHandler.ListofIds
    ServerID.Name = player.Name.. " 's Server"
    
    players.PlayerRemoving:Connect(function(plr)
        if plr == player then
            local playerServerID = ServerScriptService.ServerIDHandler.ListofIds:FindFirstChild(plr.Name.. " 's Server")
            if playerServerID then
                playerServerID:Destroy()
            end
        end
    end)
    
    return ServerID.Value
end

game.ReplicatedStorage.JoinServer.OnServerInvoke = function(player, ServerID)
    local playerServerID = ServerScriptService.ServerIDHandler.ListofIds:FindFirstChild(player.Name.. " 's Server")
    if playerServerID and playerServerID.Value == tonumber(ServerID) then
        print("Successfully joined server with ID:", ServerID)
        return true
    else
        warn("Failed to join server with ID:", ServerID)
        return false
    end
end

I have no idea if this will work, but something along these lines should work

JoinServerHandler:

local UserInputService = game:GetService("UserInputService")
local JoinBTN = script.Parent.ServerSelect.JoinServer
local JoinTextBox = script.Parent.ServerID.TextBox
local player = game.Players.LocalPlayer

JoinBTN.MouseButton1Click:Connect(function()
    local ServerID = JoinTextBox.Text
    if ServerID and ServerID ~= "" then

        local success = game.ReplicatedStorage.JoinServer:InvokeServer(ServerID)
        if success then
            print("Successfully joined the server.")
        else
            warn("Failed to join the server.")
        end
    else
        warn("ServerID is nil or empty")
    end
end)

ServerJoinCreateScript:

local ScreenGUI = script.Parent
local Players = game:GetService("Players")

local CreateBTN = ScreenGUI.ServerSelect.CreateServer
local ServerIDNum = ScreenGUI.Server.CreaterPanel["Server ID"].SERVERIDNumber
local ServerName = ScreenGUI.Server.ServerName

ScreenGUI.Server.CreaterPanel.Visible = false

CreateBTN.MouseButton1Click:Connect(function()

    local ServerID = game.ReplicatedStorage.CreateServer:InvokeServer()
    if ServerID then
        print("Created server with ID:", ServerID)

        ScreenGUI.ServerSelect.Visible = false
        ScreenGUI.Server.CreaterPanel.Visible = true
        ScreenGUI.Server.Visible = true
        ServerIDNum.Text = ServerID
        ServerName.Text = Players.LocalPlayer.Name .. " 's SERVER"
    else
        warn("Failed to create server")
    end
end)

ServerScript:

local players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local ServerIDHandler = ServerScriptService:FindFirstChild("ServerIDHandler") or Instance.new("Folder", ServerScriptService)
ServerIDHandler.Name = "ServerIDHandler"

local ListofIds = ServerIDHandler:FindFirstChild("ListofIds") or Instance.new("Folder", ServerIDHandler)
ListofIds.Name = "ListofIds"

game.ReplicatedStorage.CreateServer.OnServerInvoke = function(player)
    local ServerID = tostring(math.random(10000, 99999))
    local ServerEntry = Instance.new("StringValue")
    ServerEntry.Value = ServerID
    ServerEntry.Name = player.Name .. " 's Server"
    ServerEntry.Parent = ListofIds

    player.AncestryChanged:Connect(function()
        if not player.Parent then
            local playerServerID = ListofIds:FindFirstChild(player.Name .. " 's Server")
            if playerServerID then
                playerServerID:Destroy()
            end
        end
    end)

    return ServerID
end

game.ReplicatedStorage.JoinServer.OnServerInvoke = function(player, ServerID)
    for _, server in ipairs(ListofIds:GetChildren()) do
        if server.Value == ServerID then
            print("Successfully joined server with ID:", ServerID)
            return true
        end
    end
    warn("Failed to join server with ID:", ServerID)
    return false
end
1 Like

Nope unfortunately I did not work. :frowning: I want to keep when the player presses enter key it continues the script in Join ServerHandler. BTW I am going to sleep

JoinServerHandler:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local JoinBTN = script.Parent.ServerSelect.JoinServer
local JoinTextBox = script.Parent.ServerID.TextBox

local function onEnterPressed()
    local ServerID = JoinTextBox.Text
    if ServerID and ServerID ~= "" then
        print("Attempting to join server with ID:", ServerID)
        local success = ReplicatedStorage.JoinServer:InvokeServer(ServerID)
        if success then
            print("Successfully joined the server.")
        else
            warn("Failed to join the server.")
        end
    else
        warn("ServerID is nil or empty")
    end
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if not gameProcessed and input.KeyCode == Enum.KeyCode.Return then
        onEnterPressed()
    end
end)

JoinBTN.MouseButton1Click:Connect(function()
    onEnterPressed()
end)

ServerScript:

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerIDHandler = ServerScriptService:FindFirstChild("ServerIDHandler") or Instance.new("Folder", ServerScriptService)
ServerIDHandler.Name = "ServerIDHandler"

local ListofIds = ServerIDHandler:FindFirstChild("ListofIds") or Instance.new("Folder", ServerIDHandler)
ListofIds.Name = "ListofIds"

ReplicatedStorage.CreateServer.OnServerInvoke = function(player)
    local ServerID = tostring(math.random(10000, 99999))
    local ServerEntry = Instance.new("StringValue")
    ServerEntry.Value = ServerID
    ServerEntry.Name = player.Name .. " 's Server"
    ServerEntry.Parent = ListofIds

    player.AncestryChanged:Connect(function()
        if not player.Parent then
            local playerServerID = ListofIds:FindFirstChild(player.Name .. " 's Server")
            if playerServerID then
                playerServerID:Destroy()
            end
        end
    end)

    return ServerID
end

ReplicatedStorage.JoinServer.OnServerInvoke = function(player, ServerID)
    for _, server in ipairs(ListofIds:GetChildren()) do
        if server.Value == ServerID then
            print(player.Name .. " successfully joined server with ID:", ServerID)
            return true
        end
    end
    warn(player.Name .. " failed to join server with ID:", ServerID)
    return false
end
1 Like

I have tested it and thats the solution!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.