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!
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)