简体   繁体   English

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

[英]Lua: How do you update a variable that's in an event?

The issue I'm running into is that my variable isn't updating after an event.我遇到的问题是我的变量在事件发生后没有更新。 Here is my code:这是我的代码:

local Players = game:GetService("Players")
local intermission = 15 -- time in seconds

local AmountOfPlayers = #Players:GetPlayers() -- starts at zero
local minPlayers = 1


Players.PlayerAdded:Connect(function(Player) -- updates amount of players in server
    AmountOfPlayers = AmountOfPlayers + 1
end)

Players.PlayerRemoving:Connect(function(Player) -- updates amount of players in server
    AmountOfPlayers = AmountOfPlayers - 1
end)

Below, this if-statement is not running since AmountOfPlayers is not equal to minPlayers.下面,这个 if 语句没有运行,因为 AmountOfPlayers 不等于 minPlayers。

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

I have a scope issue of the variable AmountOfPlayers.我有一个变量 AmountOfPlayers 的 scope 问题。 I'm not sure how to fix this, so any suggestions will be appreciated.我不知道如何解决这个问题,所以任何建议都将不胜感激。 Thank you for your help!谢谢您的帮助!

You forgot to add a wait() in your while loop.您忘记在 while 循环中添加 wait() 。 This is most likely what leads to the script crashing and not working.这很可能是导致脚本崩溃且无法正常工作的原因。 So it should look like:所以它应该看起来像:

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

OR或者

If that isn't the problem, instead of making:如果这不是问题,而不是制作:

local AmountOfPlayers = #Players:GetPlayers()

Instead:反而:

local AmountOfPlayers = Players:GetChildren()

And Remove the two functions.并删除这两个功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 作为if语句的结果,如何为变量赋值? - How do you give a variable a value as the result of an if statement? 您如何处理在 function 中给出 2 个 arguments(以元组形式)的 1 个变量? - How do you process 1 variable that gives 2 arguments (in the form of a tuple) in a function? 如何在KSH if或case语句中更改变量? - How do you change a variable in a KSH if or case statement? 如何在Swift中更改IBAction UIButton的图像? - How do you change an IBAction UIButton's image in Swift? 您是否可以将if语句分配为变量,以及在计算时如何打印if语句的结果 - Can you assign an if statement as a variable and how do you print the result from an if statement when it is a calculation 在“if”括号内分配一个变量,如何在括号外使用该变量? - Assign a variable inside an “if” bracket, how do you use that variable outside the bracket? 如何访问在 IF/ELSE 范围之外创建的变量,在 IF 语句中更新它,并在 ELSE 语句中访问该更新? - How do I access a variable, created outside of an IF/ELSE scope, update it in the IF statement, and access that update in the ELSE statement? 由于 boolean 在 lua 中的当前 state,我怎么能更改它? - How could I change a boolean due to it's current state in lua? 您如何优化呢? - How do you optimize this? 如何从与特定键对应的字典中获取特定值,并将该值添加到变量中? - How do you get a specific value from a dictionary that corresponds to a specific key, and add that value to a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM