It loads two players, but doesn't load the third one?

I have this code right here:

local Players = game:GetService("Players")

while true do
	wait()
	if #Players:GetPlayers() > 1 then
		workspace.Music:WaitForChild("MainMusic"):Play()
		for i,v in pairs(Players:GetPlayers()) do
			if v:WaitForChild("Loaded").Value == true then
				v:LoadCharacter()
			end
		end
		break
	end
end

What that code does is check if there are two or more players then load all of players. Otherwise, waiting for other players to be loaded. The game will not start if there is one player only. There should be at least two players for the game to begin.

It does work when I tried with two players. However, I tried to test with 3 players, it only loads the two players, the third one is not loaded. It’s supposed to check if there are two or more players then start the game.

Any ideas why isn’t this code loading the third player?

Edit: Oh, that “Loaded” value thing you’re seeing, I created that instance then set it to true when the player is loaded, not the character.

Edit2: The “MainMusic” that’s just the background music for the game.

1 Like

the time it reaches to the #players:Getplayers > 1 it breaks the while loop technically it goes outside of the while loop so when there is more people than 1 in the game it breaks the while loop

2 Likes

If I don’t use break, it will spam the LoadCharacter() and other things that will command the game to begin.

1 Like

Then maybe make boolean to check if the player is already loaded or not make the bool value true after loading them and in while loop firstly check if the player is loaded if no loaded then do the main code else just let the while loop go its way

2 Likes

Also but i dont recommend using while loop to check something super fast just use game.Players.PlayerAdded to check whenever new player joins game

2 Likes

That could work. Lemme try it first.

1 Like

Hey, I remember you from that one post about a tween train system lagging, I’m actually making a train system right now, anyways back to the point. I think you could do something like this:

local Players = game:GetService('Players')

local game_running = false --// You can change this variable when needed

coroutine.wrap(function()
    while wait() do
        if #Players:GetPlayers() > 1 then
            if not game_running then
                 game_running = true
                 
                 for i, Player in pairs(Players:GetPlayers()) do
                     local Character = Player.Character

                     if Character then
                         --// do what you want here.
                     end
                 end
            end
        end
    end
end)()

I’m using a coroutine here in case you need to do other stuff with the script

Same thing happens, I tried printing, it does print 1 and 2 when the players start loading in.
When the third one loads, it kinda stops the loop.

1 Like

Oh wait you edited it I could try this

1 Like

No no no. I disabled the part when the game automatically loads the characters when the player loads in.

1 Like

If you’re making something like a minigame system, I suggest you do something like so

local Players = game:GetService('Players')
local ServerStorage = game:GetService('ServerStorage')

local GameRunningValue = ServerStorage:WaitForChild('GameRunning') --[[have a bool value in the server storage 
that tells if the game is running]]

while true do
    if #Players:GetChildren() <= 1 then
        --[[ this part checks if there aren't multiple people and if so,
 loop and wait until that's not the case anymore]]
        while #Players:GetChildren() <= 1 and wait() do end
    end

    for i, Player in pairs(Players:GetChildren()) do
        if Player:WaitForChild('Loaded').Value then
            Player:LoadCharacter()
        end
    end

    GameRunningValue.Value = true
    workspace.Music:WaitForChild("MainMusic"):Play()

    while GamingRunningValue.Value and wait() do
        --[[Do the game here, if the game ends, set the value to false
            and it will exit this loop]]
    end

   --// If the loop above stopped because the game ended, this will loop back to the top of the main loop
end
1 Like

@BowlerAr But I’m trying to check if there are 2 or more players. If there are 3 players, then three of them should have their characters loaded. But the problem is it only calls LoadCharacter() on two players even if there are three loaded.

1 Like

Are 3 of them even loaded? add a print statement that runs whenever the Player:WaitForChild('Loaded').Value statement is satisfactory

1 Like

Btw this is unrelated, but for my train system the lag only occurs when the train goes too fast so I’m making the game scaled down (and i have a script that scales the players down as well) and I just divide the speed

1 Like

@BowlerAr I’m sure they are loaded, I even checked from the explorer, took a look on every Loaded bool, it has been set to true.

In a separate script, whenever a player joins, an Instance is created which is bool value, then name it Loaded and set it to true.

1 Like

Well then maybe the 3rd player is loading after the code has ran

1 Like

First of all I use the thing in roblox studio where you can go “multiplayer mode” and choose how many players you want.

Pretty sure all three of them are loaded at the same time. But the script doesn’t notice that Player3 has been loaded too. Resulting that player 1 and 2 characters are loaded only.

1 Like

Hmmm, maybe add a wait(10) at the start of the script, also roblox studio test mode is slower than the roblox player, especially with mutliple clients.

1 Like

Maybe that’d work. I’m not on PC right now so I’ll try it tomorrow.

1 Like