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

How do I only allow the game to start with 2 or more players?

Asked by
z_lm 9
4 years ago

This script works even when there's only one person, and I want it to only work when there's greater than or equal to 2. I keep adding scripts to do so, but everytime I do, the GUI stops working. Any help please?

local roundLength = 10
local intermissionLength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local LobbySpawn = game.Workspace.Lobby
local GameAreaSpawn = game.Workspace.GameArea
InRound.Changed:Connect(function()

    if InRound.Value == true then

        for _, player in pairs(game.Players:GetChildren()) do

            local char = player.Character

            char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame

        end

    else

        for _, player in pairs(game.Players:GetChildren()) do

            local char = player.Character

            char.HumanoidRootPart.CFrame = LobbySpawn.CFrame

        end

    end

end)

local function roundTimer()

    while wait() do

        for i = intermissionLength, 0, -1 do

            InRound.Value = false

            wait(1)

            Status.Value = "Intermission: ".. i .." seconds left!"

        end

        for i = roundLength, 0, -1 do

            InRound.Value = true

            wait(1)

            Status.Value = "Game: "..i.." seconds left!"

        end

    end

end



spawn(roundTimer)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
repeat -- Create a loop to check number of players
    wait()
    local numberofplayers = #game.Players:GetChildren() -- Instance:GetChildren() returns a table of all children. You can use # to get the number of items in a table, so combining them into a definition will return the number of children inside the players folder, which is how many players there are.
    if numberofplayers >= 2 then -- Check if number of players is greater than or equal to 2
        spawn(roundTimer) -- if so run function
        break -- break the repeat loop so it doesn't keep running.
    end
until #game.Players:GetChildren() >= 2 
0
Gah! Im so sorry, but I just need a little more time to figure out where to put this. If I put it at the end it breaks the gui, but if I put it at the start, there isn't a "spawn(roundTimer"). z_lm 9 — 4y
0
Inside the script you posted on line 63 is spawn(roundTimer). This is what tells the function to run, replace that with what I provided WizyTheNinja 834 — 4y
0
Oh it works! Thank you! It threw me off how the text was just blank, I just tested the game with me and an alt. Do you think instead of repeat, instead it should fire everytime a player leaves/joins? Because once that person leaves, the person alone keeps playing. z_lm 9 — 4y
0
Yeah probably, I mainly was just showing you how to do it. Changing it to when they leave/join wont stop the game when its only 1 player however, you would have to code that yourself. Check if players is >2 if not teleport players back to lobby and restart timer or something like that. WizyTheNinja 834 — 4y
Ad

Answer this question