Search code examples
luaroblox

Why doesn't this code work in roblox studio? I'm new and I don't know why it gives me error


local contador = 1 
local jugadores = game.Players
while jugadores > 3 do
while contador > 10 do
    contador = contador + 1
    print("Quedan " ..contador.. "segundos")
    wait(1)
print( "Hay "..jugadores.. "jugadores" ) 
end
end

It gives me the error in bold and the parenthesis

I tried to quit the parenthesis but I'm new and I don't know what to do. Thanks


Solution

  • Because you are using a Script in the Workspace, it will execute as soon as it is loaded into the world. So when this code runs it will go something like this :

    • there are no players in the world...
    • is jugadores greater than 3? no, skip the loops...
    • exit

    So you need to have a way to wait for players to join the game before you execute your logic. I would recommend using the Players.PlayerAdded signal as a way to check when there are enough players.

    local Players = game:GetService("Players")
    
    -- when a player joins the game, check if we have enough players
    Players.PlayerAdded:Connect(function(player)
        local jugadoresTotal = #Players:GetPlayers()
        print(string.format("Hay %d jugadores", jugadoresTotal))
    
        if jugadoresTotal > 3 then
            local contador = 1
            while contador <= 10 do
                print(string.format("Quedan %d segundos", contador))
                wait(1)
                contador = contador + 1
             end
    
             -- do something now that we have 4 players
        end
    end)