Problem with simulating player flow in Roblox Studio

Hello everyone. Quick question. I am currently testing my game with the “Local Server” feature on Studio. The script I am facing issues with is along the lines of:

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(char)
      if #game.Players:GetPlayers() == 2 then
        print('enough players')
      end
   end)
end)

Now the output would currently look like this:
enough players
enough players

Is this due to latency issues due to the local server loading faster than the Roblox cloud servers or something wrong in the script format? Thanks! :slight_smile:

Try this:

for i, plr in pairs(game.Players:GetPlayers()) do
if i >= 2 then
print("enough players")
end

end

I am not sure this would give me the desired result. The print message would be printed multiple times if there’s more people in the server. I only want this to happen just once when the server has reached 2 players. Even by changing the comparison operator, this would still output similar results as the one provided in the dummy script.

Shouldnt spam the output, test it tho, i may be wrong.

The double message happens because both players join first and then their characters are added causing the double fire. The message will also happen if one of the players respawn but im assuming that’s on purpose.

1 Like

something like

repeat task.wait() until #game.Players:GetPlayers() >= 2
print("Enough")

Also, @mniao has properly explained why you are getting multiple prints.

2 Likes

Thanks for your response. This basically confirms my initial theory. Your code also solves the output spam, but the local server still has some wonky stuff in it… although a bit less performant, at least it gets the job done. Will likely have to run my tests with an alt account on a cloud server.

cc: @mniao

1 Like

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