How to call functions in other script files in Roblox

Slim picture Slim · Jun 28, 2009 · Viewed 19.1k times · Source

I have a script file embedded in the Workspace that contains functions. I would like call these functions from script files embedded in child objects of the Workspace. I don't want to have to copy and paste these functions into multiple script files. I figured the object oriented approach would be best if its possible.

Answer

blacksmithgu picture blacksmithgu · Nov 23, 2010

An alternative to _G is to use the also globally avaliable table, shared. Shared is used the same way _G is, but you must specify "shared" before the variable identifier, unlike _G, where you can merely write the name of the variable without the _G (not anymore in ROBLOX). Shared is used in the following context:

shared["variablename"] = value

Just like in the global stack, _G. Example usage of shared:

Script 1

shared["rprint"] = function(array) for i,v in pairs(array) do print(i, v) end end

Script 2

repeat wait() until shared["rprint"]
shared.rprint({"Hello, ", "How", " are", " you?"})

The output of this script would be "Hello, How are you?"