Lua:如何在事件中更新变量?

我遇到的问题是,在事件之后我的变量没有更新。下面是我的代码:

local Players = game:GetService("Players"local intermission = 15 -- 时间(秒)

local AmountOfPlayers =#Players:GetPlayers()--从零开始
local minPlayers = 1

Players.PlayerAdded:Connect(functionPlayer) - 更新服务器中的玩家数量
    AmountOfPlayers = AmountOfPlayers + 1
endPlayers.PlayerRemoving:ConnectfunctionPlayer)-更新服务器中的玩家数量
    AmountOfPlayers = AmountOfPlayers - 1
end

下面,由于AmountOfPlayers不等于minPlayers,所以此if语句不运行。

while intermission> 0 do
    if AmountOfPlayers> = minPlayers then
    print(“SUCCESS”)
    end
end

我在变量AmountOfPlayers的范围问题方面存在问题。 我不确定如何解决此问题,因此将不胜感激地接受任何建议。 谢谢您的帮助!

点赞
用户14208589
用户14208589

你忘记在 while 循环中加入 wait()。这很可能导致脚本崩溃而无法正常工作。所以应该这样写:

while intermission > 0 do
    wait()
    if AmountOfPlayers >= minPlayers then
    print("SUCCESS")
    end
end

或者

如果这不是问题所在,而是替换:

local AmountOfPlayers = #Players:GetPlayers()

为:

local AmountOfPlayers = Players:GetChildren()

并且移除这两个函数。

2020-09-02 13:02:18