Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

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.

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

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;

3 comments

1

second and third examples are incorrect, cannot compare number to a table

1

Most of this code is redundant.

0

You didn't explain anything.

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.

2 comments

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.

0

You didn't explain anything.

This discussion is preserved as it was. New answers, comments, and votes are closed.