I'm having trouble and I have tried multiple types of loops but nothing seems to work, basically I am trying to have a
1 | while true do |
2 |
3 | -- if 2 players have 'CanPlay' set to true then |
4 | -- continue code. |
which runs through the players and searches to see if their value "CanPlay" boolvalue is set to true. I know this sounds like a request, but it's not.. and I tried! Please leave help if you can!
My attempts:
01 | if game.Players.NumPlayers > = PlayerNumber and game.Players.NumPlayers.CanPlay = = true then then -- Wasn't sure.. |
02 |
03 | -- Tried using loops, the while true do loop is the entire game I have setup. |
04 |
05 | while true do |
06 |
07 | if game.Players.NumPlayers > = PlayerNumber then |
08 | for i, v in pairs (game.Players:GetPlayers()) do |
09 | if v.CanPlay.Value = = true then |
10 | -- Not sure how to continue. |
11 | end |
12 | end |
13 |
14 | -- Code |
15 | else |
16 | end |
First off, I'd split this into two functions, one to check if there are enough players with CanPlay set to true, and another to loop until the first function returns true.
1 | function enoughPlayers() |
2 | end |
3 |
4 | function waitUntilEnoughPlayers() |
5 | while not enoughPlayers() do wait() end |
6 | end |
Okay, now to make the enoughPlayers
function work. You're off to a good start with looping through each player and checking who has CanPlay set to true. Next I'd make a number variable count up for each player that has CanPlay set to true.
01 | function enoughPlayers() |
02 | local playing = 0 |
03 | for i, v in pairs (game.Players:GetPlayers()) do |
04 | local CanPlay = v:FindFirstChild( "CanPlay" ) -- let's be safe and check if the CanPlay BooleanValue exists first aswell |
05 | if CanPlay and CanPlay.Value then |
06 | playing = playing+ 1 |
07 | end |
08 | end |
09 | return playing > = 2 -- this makes the function return true if the playing variable is greater than or equal to 2, otherwise it will return false |
10 | end |