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.
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
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
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)()
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
@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.
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
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.
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.