How to call functions in other scripts?
Asked Answered
D

7

11

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.

Dactylo answered 28/6, 2009 at 19:7 Comment(1)
Just to be noted is that BindableFunctions and RemoteFunctions might be of interest now; remote functions even enable you to call functions between server and clientSupercool
C
7

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?"

Crossfertilize answered 22/11, 2010 at 23:19 Comment(4)
Voted up as the _G variable has been removed from the Roblox Scripting API. This answer shows the table that is now used to 'share' values between scripts.Flowering
Good. People should know about shared, as it's pretty important when you have no other alternative.Crossfertilize
The wiki says you can still use _G wiki.roblox.com/index.php/Function_Dump/Core_Functions#GAssassinate
@JonLyles "it was possible to read values from it without writing _G in front. This is no longer the case." (from the wiki)Supercool
W
3

The simplest way would be to use _G or shared.

In one script,

_G.myFunction = function(Arguments)

-- blah

end

In another script, you would use this code.

repeat wait() until _G.myFunction ~= nil

_G.myFunction()

This would also work with the global table shared, instead of _G.

Walkway answered 22/11, 2010 at 20:46 Comment(0)
F
2

You can make the function global. In one script do this:

_G.myFunction = function() print("Hello World") end

In another script do this:

repeat wait() until myFunction myFunction()

By defining a function is _G you must wait for the script to execute assigning the function, then you can call the function even without specifying _G.

This won't work because, due to ROBLOX updates, you now have to index _G whenever you access items inside it.

You cannot use dofile() in ROBLOX, either, as I saw mentioned above.

In answer to the question: you need to create a function in one script into the global table - _G, by adding _G.MyFunction = function(parameters) end. In another script, you need to access it inside the _G table - _G.MyFunction().

A common problem that appears for ROBLOX scripters is that you try to access your function inside _G before it is created. A simple way to solve this is to add a wait until it is created, as suggested from Camoy's post:

repeat wait() until _G.MyFunction() 
Frantic answered 22/11, 2010 at 20:2 Comment(0)
Z
2

I know it has been said before, but just use the normal _G or shared to access it.

Script one

_G.myFunction = function()
     print("Hello, myFunction!")
end

Script two

repeat wait() until _G.myFunction()
_G.myFunction()

Output

Hello, myFunction!

Zeb answered 22/11, 2010 at 21:48 Comment(0)
N
2

I would use BindableEvents or RemoteEvents. I think this is a better approach than using _G. This will allow you to keep everything local. You can use Bindableevents and RemoteEvents to trigger functions and send as much data as you need back and forth between scripts. Use BindableEvents for server/server communication and RemoteEvents for server/client-client/server communications.

http://wiki.roblox.com/index.php?title=API:Class/BindableEvent

Newfoundland answered 2/3, 2016 at 18:17 Comment(0)
E
1

You can make the function global. In one script do this:

_G.myFunction = function() print("Hello World") end

In another script do this:

repeat wait() until myFunction myFunction()

By defining a function is _G you must wait for the script to execute assigning the function, then you can call the function even without specifying _G.

Encomium answered 17/7, 2010 at 12:35 Comment(1)
then you can call the function even without specifying _G not anymoreSupercool
B
1

You could use Module Scripts which were thankfully added. You could put the functions in there, then call and use them anywhere else!

Bryan answered 22/12, 2016 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.