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 make my game not start until there are a certain number of players in the server?

I wanted to have the game I made not start until there are at the very least 2 players in the game. I have tried:

game:GetService("Players").PlayerAdded:Connect(function()
	if (#game:GetService("Players"):GetPlayers() >= 2) then
		-- Start the game
	else
		-- Tell the player that there needs to be at least 2 players to start
	end
end)

The thing I am worried about is that if there are 2 players in the game and another player joins, the game would start from the beginning again. How would I modify the script in order to start the game when there are at least 2 players and will not start again if more join?

EDIT:

I have decided to use a boolean saved in a variable called debounce. This way when there are 2 players the game can start and if more join, the game will not restart again.

debounce = true

game:GetService("Players").PlayerAdded:Connect(function()
	if (#game:GetService("Players"):GetPlayers() >= 2 and debounce) then
		debounce = false
		-- Start the game
	else
		debounce = true
		-- Tell the player that there needs to be at least 2 players to start
	end
end)

If there are other solutions, please let me know!

4 comments

0

syntax error at line 2; you're missing a parenthesis

0

Fixed

0

You can use a while loop for this. The loop will keep running as long as there are at least 2 players in the game.

1

I have decided to use a boolean inside a variable called debounce to fix the issue

1 answer

✓ Accepted answer

In order to prevent it from restarting, you must use a debounce. A debounce is a variable that tells the code to not repeat itself after running once. An example of a debounce is like so:

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
 if debounce then return end -- Prevents the code from running if true
 debounce = true -- allows the script to know it already ran once
 print(“on”)
end)

2 comments

1

Yeah I realized that when reviewing my code again

1

Just realized you replied to it saying you did that

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