Do a counter to make the game start in Roblox Studio

Viewed 88

I want to make a counter to make the game start, but my code doesn't works, I have a text label and inside it this script

local plrcount = 0 -- Define the number at first.

for i,v in pairs(game.Players:GetChildren()) do
    if v:IsA("Player") then -- Check if it's a player.
        plrcount = plrcount+1 -- Update the player count.
    end
end

contador = 10

if plrcount > 1 then
    while contador == 0 do
    contador = contador - 1
    end
end


function updatevalue()
    script.Parent.Text = contador
end

script.Parent.Value.Changed:Connect(updatevalue)
2 Answers

You only have it checked when you run the code, use a function (example: onChange).

I see multiple issues. 1: This only runs one time. You need to put this inside of a loop in order for it to run twice, or for it to run when player count is changed. 2: If there is over one player, the contador variable will reach zero instantly, because it does not wait. 3: I'm not sure if there is a child of the label called Value, but if there isn't, you do not use .Changed with .Value. It is a property of the Label itself. 4: plrcount never rests, so it is constantly going up when players join. 5: if this is starting a game, it should be managed on the server.

TL:DR code runs one time, timer is instant, .Changed doesn't work on a Value, and this should be server side.

This worked for me (script is a regular script inside of ServerScriptService. This could definitely be cleaned up, but I tried to keep a format similar to your old script.):

local plrcount = 0 -- Define the number at first.

contador = 10

while true do
    wait(1)
    plrcount = 0
    for _, v in pairs(game.Players:GetChildren()) do
        if v:IsA("Player") then -- Check if it's a player.
            plrcount = plrcount+1 -- Update the player count.
        end
    end
    if plrcount > 1 and contador > 0 then
        contador -= 1
    end
    
    for _, v in pairs(game.Players:GetChildren()) do
        if v:IsA("Player") then -- Check if it's a player.
            v.PlayerGui:WaitForChild("ScreenGui").TextLabel.Text = contador
        end
    end
    
end
Related