Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I delay a script until there are 3 or more players in a game???

Asked by 7 years ago

I don't have my recent attempt, but I have tried to do this myself. I'm making a minigame, I need it to wait until there are 3 or more players in a game, then begin the minigame.

4 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Here's an event-driven approach, because they're always better than running a loop until some condition changes.

01local Players = game:GetService("Players")
02local ThreePlayers = Instance.new("BindableEvent").Event -- Get the event interface from the object
03 
04-- When a player joins, compare the number of players in-game with your minimal quantity.
05local function PlayerUpdate()
06    -- I was told the 'NumPlayers' property of Players was deprecated, so until (or if) they release something similar, the most reliable way to get the number of players is getting the length of the array playing.
07    if #Players:GetPlayers() >= 3 then
08        ThreePlayers:Fire() -- Fire our new artificially created event
09    end
10end
11 
12-- Function binded to our ThirdPlayer event
13local function ThirdPlayerAdded()
14    print("Game started")
15end
View all 26 lines...

Much better. Don't forget that you're able to create events the same way ROBLOX implements them. It can lead to much more efficient, and versatile code that you won't later regret.

0
In retrospect you may want to call the event "ThreePlayers" or something. "ThirdPlayer" could imply it will fire every third player that joins, when in actuality it only fires when there are three players in the game. ScriptGuider 5640 — 7y
Ad
Log in to vote
2
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

we can repeat wait until the number of children that are in the game is greater or equal than 3

1repeat wait() until #game.Players:GetChildren()>=3
Log in to vote
-2
Answered by
Hybric 271 Moderation Voter
7 years ago
Edited 7 years ago

Here's some help. However, please realize that this site is not for requesting.

01function calculateNumPlayers()
02    local count = 0;
03        for i,v in pairs(game.Players:GetChildren()) do
04        count=count+1;
05        end
06    return count;
07end
08 
09local c = 0;
10 
11while c < 3 do
12    wait(1)
13    c = calculateNumPlayers()
14end

Another way to do this that is understandable is

1while game.Players:GetChildren()<3 do
2wait(1)
3end

You can also do

1repeat wait(1) until game.Players:GetChildren()>2;
1
second and third examples are incorrect, cannot compare number to a table DanzLua 2879 — 7y
1
Most of this code is redundant. ScriptGuider 5640 — 7y
0
You didn't explain anything. Goulstem 8144 — 7y
Log in to vote
-2
Answered by 7 years ago
Edited 7 years ago
01local numplayers = 0
02game.Players.PlayerAdded:Connect(function()
03    numplayers = numplayers + 1
04        if numplayers >= 3 then
05    --your script here
06        end
07end)
08game.Players.PlayerRemoving:Connect(function()
09    numplayers = numplayers - 1
10end)

that should work. if not, tell me. i typed it here.

1
Changing increments isn't necessary in this case - you could just simply get how many players are in a lobby each time a new one joins, or leaves. ScriptGuider 5640 — 7y
0
You didn't explain anything. Goulstem 8144 — 7y

Answer this question