简体   繁体   English

如何在 roblox studio 中更新模块脚本的变量?

[英]How do I update a module script's variables in roblox studio?

So I have scriptOne (module) and scriptTwo (local script I think).所以我有scriptOne (模块)和scriptTwo (我认为是本地脚本)。 I use scriptTwo to require() and change the variables with that.我使用scriptTworequire()并用它来更改变量。 But how do I update the values in scriptOne (that I also want to access from other scripts as well) to these new values?但是如何将scriptOne中的值(我也想从其他脚本访问)更新为这些新值?

scriptOne code脚本一个代码

local module = {}

module.test = 100

while true do 
    wait(1)
    print(module.test)
end

return module

scriptTwo code脚本二代码

local data = require(workspace.playerStats)

data.test = 0

changing variables inside of a modulescript is global, change a variable and every script using the module gets the new variable更改模块脚本内的变量是全局的,更改变量并且使用该模块的每个脚本都会获得新变量

the reason why the code you provided doesnt work is because of the while loop.您提供的代码不起作用的原因是因为 while 循环。 the loop yields and cant proceed, meaning it cant also return the module so the server script is waiting forever.循环产生并且无法继续,这意味着它也不能返回模块,因此服务器脚本永远等待。

The problem is the while loop in the ModuleScript.问题是 ModuleScript 中的 while 循环。 ModuleScripts are used by require() to share variables. require()使用 ModuleScripts 来共享变量。 In your case, your ModuleScript has a infinite loop, which doesn't let require() return, and gets the second script stuck.在您的情况下,您的 ModuleScript 有一个无限循环,它不会让require()返回,并让第二个脚本卡住。 You are probably using the while loop for debugging the variable, so I'd recommend moving it to the second script.您可能正在使用 while 循环来调试变量,因此我建议将其移至第二个脚本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM