[英]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.