简体   繁体   中英

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

So I have scriptOne (module) and scriptTwo (local script I think). I use scriptTwo to require() and change the variables with that. 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 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. 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. ModuleScripts are used by require() to share variables. In your case, your ModuleScript has a infinite loop, which doesn't let require() return, and gets the second script stuck. You are probably using the while loop for debugging the variable, so I'd recommend moving it to the second script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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