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

How can I make my game not start until there are a certain number of players in the server?

Asked by 4 years ago
Edited 4 years ago

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!

0
syntax error at line 2; you're missing a parenthesis DeceptiveCaster 3761 — 4y
0
Fixed skate992 57 — 4y
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. DeceptiveCaster 3761 — 4y
1
I have decided to use a boolean inside a variable called debounce to fix the issue skate992 57 — 4y

1 answer

Log in to vote
0
Answered by
poke7667 142
4 years ago

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)
1
Yeah I realized that when reviewing my code again skate992 57 — 4y
1
Just realized you replied to it saying you did that poke7667 142 — 4y
Ad

Answer this question