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 6 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 6 years ago
Edited 6 years ago

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

local Players = game:GetService("Players")
local ThreePlayers = Instance.new("BindableEvent").Event -- Get the event interface from the object

-- When a player joins, compare the number of players in-game with your minimal quantity.
local function PlayerUpdate()
    -- 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.
    if #Players:GetPlayers() >= 3 then
        ThreePlayers:Fire() -- Fire our new artificially created event
    end
end

-- Function binded to our ThirdPlayer event
local function ThirdPlayerAdded()
    print("Game started")
end

-- Connect functions to event listeners
ThreePlayers:Connect(ThirdPlayerAdded)
Players.PlayerAdded:Connect(PlayerUpdate)
Players.PlayerRemoving:Connect(PlayerUpdate)

-- Now anytime you want to wait for at least three players in the game, you'd just need to call Wait on the event:

print("Waiting for at least three players...")
ThreePlayers:Wait()
print("Game has at least three players")

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 — 6y
Ad
Log in to vote
2
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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

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

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

function calculateNumPlayers()
    local count = 0;
        for i,v in pairs(game.Players:GetChildren()) do
        count=count+1;
        end
    return count;
end

local c = 0;

while c < 3 do
    wait(1)
    c = calculateNumPlayers()
end

Another way to do this that is understandable is

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

You can also do

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

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 — 6y
0
You didn't explain anything. Goulstem 8144 — 6y

Answer this question