Everyone in the game automatically joins a server/lobby

Hello!

I’ve got a bug in my game where everyone in the game joins a player’s server automatically. I have no idea what is causing this issue and what code is leading to this issue. It may be these two scripts, a local and server script.

Video:

Video Key Moments:

  • Player 1 creates a server
  • Player 2 is automatically joined the server
  • Then confiremed that player 2 did not join the server on their screen.

There are more scripts in my game, if these scripts don’t look like the issue, I will give more.

Local script:

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.CreateServers.CreateServer:InvokeServer()
	if ServerID then
		print("Created server with ID:", ServerID)

		ScreenGUI.Server.Position = UDim2.new(0.5, 0,0.365, 0)
		ScreenGUI.ServerSelect.Visible = false
		ScreenGUI.Server.CreaterPanel.Visible = true
		ScreenGUI.Server.Visible = true
		ServerIDNum.Text = ServerID
		ServerName.Text = Players.LocalPlayer.Name .. " 's SERVER"
		
		game.ReplicatedStorage.CreateServers.CreateServerServer:FireServer(Players)
	else
		warn("Failed to create server")
	end
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.CreateServers.CreateServer.OnServerInvoke = function(player)
	 
	print("CreateServer")
	
	local ServerID = tostring(math.random(10000, 99999))
	local ServerEntry = Instance.new("StringValue")
	ServerEntry.Value = ServerID
	ServerEntry.Name = player.Name .. " 's Server"
	ServerEntry.Parent = ListofIds
	

	local ServerName = Instance.new("StringValue")
	ServerName.Name = player.Name
	ServerName.Value = player.Name.. "'s SERVER"
	ServerName.Parent = ServerScriptService.ServerIDHandler.ServerNames
	-- set the parent as the last property because it's more optimized

	for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
		game:GetService("ReplicatedStorage").ServerNames:FireClient(ReceivePlayer, player.Name, ServerName.Name)
		print("for loop")
	end
	
	


	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.JoinServers.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



ReplicatedStorage.CreateServers.CreateServerServer.OnServerEvent:Connect(function(player)
	ReplicatedStorage.CreateServers.CreateServerPlayer:FireAllClients(player)
end)

ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
	ReplicatedStorage.CloseFunction.LeaveServer:FireAllClients(player)
end)

All help would be well appreciated!!!

3 Likes

Hello! You can check if the player is listed as being on the server created only for the player 1 client or for all server?

What do you mean by that?? (Filling character limit)

How I solved this problem:

  • Make a remote function
  • Make a variable and set it to false when the player joins the game
  • When a player joins a server it sets it to true and runs the following code
game.ReplicatedStorage.PLRNames.OnClientEvent:Connect(function(plrname) 
	if InServer == true then
		print(tostring(plrname)) 
		local playerTextClone = PlayerNameText:Clone()
		playerTextClone.Text = tostring(plrname)
		playerTextClone.Parent = script.Parent
		playerTextClone.Name = plrname
		InServer = false
	else
		print("Player is not in server")
	end
	
	
end)

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