为什么这段代码在 roblox studio 中不起作用?我是新人,我不知道为什么它给我错误

问题描述 投票:0回答:2
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

它给了我粗体和括号中的错误

我试图退出括号,但我是新手,我不知道该怎么办。 谢谢

lua roblox
2个回答
1
投票

是 - 将字符串连接在一起使用两个点而不是两个星号。
所以使用

..
而不是:
**

http://www.lua.org/pil/3.4.html


0
投票

因为您在工作区中使用脚本,所以一旦加载到世界中,它就会立即执行。所以当这段代码运行时,它会变成这样:

  • 世界上没有玩家...
  • jugadores
    大于3吗?不,跳过循环...
  • 退出

所以你需要有一种方法来等待玩家加入游戏,然后再执行你的逻辑。我建议使用 Players.PlayerAdded 信号 作为检查何时有足够玩家的方法。

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)
© www.soinside.com 2019 - 2024. All rights reserved.