如何在Roblox Studio中更新模块脚本的变量?

我有scriptOne(模块)和scriptTwo(我认为是本地脚本)。我使用scriptTworequire()并更改这些变量。但是如何将scriptOne中的值更新到这些新值(我也想从其他脚本中访问)?

scriptOne代码

local module = {}

module.test = 100

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

return module

scriptTwo代码

local data = require(workspace.playerStats)

data.test = 0

原文链接 https://stackoverflow.com/questions/69582992

点赞
stackoverflow用户16667848
stackoverflow用户16667848

原文:

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. the loop yields and cant proceed, 
meaning it cant also return the module so the server script is waiting forever.

翻译:

在一个模块脚本中更改变量是全局的,更改一个变量后,使用该模块的每个脚本都会得到新的变量。

你提供的代码之所以不起作用,是因为其中有一个 while 循环。循环会 yield,导致无法继续运行,也就无法返回模块,因此服务器脚本会一直等待。

2021-10-15 16:31:00
stackoverflow用户14551413
stackoverflow用户14551413

问题出在 ModuleScript 中的 while 循环。ModuleScript 用于 require() 共享变量。在你的情况下,你的 ModuleScript 有一个无限循环,这不让 require() 返回,并导致第二个脚本被卡住。你可能正在使用 while 循环来调试变量,所以我建议将其移到第二个脚本中。

2022-08-04 21:58:32