Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to program with shared memory & threads?

Asked by
Ribasu 127
6 years ago

As we all know, using spawn() will allow us to execute functions in parallel. However, how can we share memory so that what happens on one function can be checked by the other function?

I suppose one of the answer is using global scope variables with _G but this will make it global thru all scripts. What are some other ways and perhaps with more restricted scope?

0
You could use RemoteFunctions or BindableFunctions to share values between scripts. KingLoneCat 2642 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You could use modulescripts for this. Unlike what KingLone said, you can not "share" variables with RemoteFunctions or BindableFunctions, only the values of those variables. You can create a module that returns variables, and only scripts that are able to require it will be able to acces- and change those values

For example, within a ModuleScript:

local module={

    a=true,
    b="hi!",
    c=4,

}

return module

Inside a serverscript:

local returnedTable=require(modulescript)

print(returnedTable.a)
print(returnedTable.b)
print(returnedTable.c)
Ad

Answer this question