The question basically speaks for itself, this would be very useful in my new game.
Okay this is rather simple
_G.FunctionName = function(Variables) -- Define the function -- Whatever you want to do end
In another script
_G.FunctionName(Variables)
This is fairly simple to do. And that is using a ModuleScript. To simplify things; a ModuleScript is a special kind of script that can be obtained using the require()
function from any script, anywhere (as long as the script can obtain it. ex: a LocalScript cannot access ServerStorage - so it would error.)
ModuleScripts work in the same manner as normal Scripts, but functions are named slightly different. I'll show you an example:
-- ModuleScript local module = {} -- any functions/variables in here will be part of the module. function module.printMyFavouriteAnimal() print("probiker123's favourite animal is a dog!") end return module
As you may notice, the function name is slightly different than what you'd expect. But it's very simple to understand. Rather than using function functionName()
, instead you use function moduleName.functionName()
so since "module" is the module, the function would be module.functionName()
.
Now that you have the ModuleScript setup, it's pretty straightforward from here. In ANY script (that, of course, can access it) you simply type, local moduleName = require(script location)
. This requires the module. Now to run the function from the script, you type the variable name (that you created within the server/local script) and then the function name. So here's how you'd do it.
-- Server Script local myModule = require(game.ReplicatedStorage:WaitForChild("ModuleScript")) myModule.printMyFavouriteAnimal()
> "probiker123's favourite animal is a dog!"
Hope this helped!