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

How do I use a local GUI script run a function from another script when I press a button?

Asked by 6 years ago

So I made a function that I need for my game & it is supposed to be called when I push a button on the screen GUI but since the code for the GUI is on a local script I don't know how to make it call a function from another script.

BTW I have made the function into a global function but if necessary I can turn it back into a regular function.

0
Have you tried module scripts? zor_os 70 — 6y

1 answer

Log in to vote
1
Answered by
oilsauce 196
6 years ago

ModuleScripts!

ModuleScripts is a Lua source container, that is only ran once, and can only return one value.

1-- ModuleScript
2 
3local module = {}
4 
5return module

How do we use these? I have a couple of examples:

1-- ModuleScript
2 
3local myFunc = function() print("I am a function from a ModuleScript.") end;
4 
5return myFunc
6 
7-- The ModuleScript is placed in ReplicatedStorage, and it is named 'myModuleScript'.

The above code will return myFunc, which is a function that will print "I am a function from a ModuleScript." when it's called, defined as a variable. Now, to run this function from a Script (Same with LocalScripts).

01-- Script
02 
03local repstore = game:GetService("ReplicatedStorage")
04local myReturnedFunc = require(repstore:WaitForChild("myModuleScript"))
05 
06myReturnedFunc()
07 
08--[[ Expected Output
09 
10I am a function from a ModuleScript.
11]]

To get the returned value from a ModuleScript, we use require. For more information about this, you can take a look at this link: https://www.robloxdev.com/articles/Built-in-Functions-and-Variables/Roblox

myReturnedFunc is the returned function which was contained in the ModuleScript (myModuleScript). And to call the function of course, we do myReturnedFunc().

The best thing about this, is that you can call this function from any Script/LocalScript aslong as the function is contained in a ModuleScript, and it is returned.

What if you want to use multiple functions in various scripts?

We use a table, and contain all the functions you want to call in it. There are two ways to put a function in a table:

01-- ModuleScript
02 
03local myTable = {
04 
05myFunc = function()
06print("I am a function inside a table.")
07end;
08 
09myFuncTwo = function()
10print("I am the second function inside the same table.")
11end;
12}
13 
14return myTable

or,

01-- ModuleScript
02 
03local myTable = {}
04 
05function myTable.myFunc()
06print("I am a function inside a table.")
07end;
08 
09function myTable.myFuncTwo()
10print("I am the second function inside the same table.")
11end;
12 
13return myTable

How you call these functions are similar to the last way, except you need to get the functions from the returned table.

01-- Script
02 
03local repstore = game:GetService("ReplicatedStorage")
04local myReturnedTable = require(repstore:WaitForChild("myModuleScript")) -- This returns the table we made in the ModuleScript (myTable).
05 
06myReturnedTable.myFunc()
07myReturnedTable.myFuncTwo()
08 
09--[[ Expected Output
10 
11I am a function inside a table.
12I am the second function inside the same table.
13]]

I hope I helped in any way, thanks for reading.

If you want to take a look at the wiki about this, here's the link: https://www.robloxdev.com/api-reference/class/ModuleScript

Ad

Answer this question