How to call functions in other script files in Roblox

22,892

Solution 1

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

Solution 2

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.

Solution 3

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!

Solution 4

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() 

Solution 5

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

Share:
22,892

Related videos on Youtube

Slim
Author by

Slim

Updated on August 07, 2020

Comments

  • Slim
    Slim almost 4 years

    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.

  • Nic
    Nic over 13 years
    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.
  • blacksmithgu
    blacksmithgu over 13 years
    Good. People should know about shared, as it's pretty important when you have no other alternative.
  • Jon Lyles
    Jon Lyles over 11 years
  • ZombieSpy
    ZombieSpy over 9 years
    @JonLyles "it was possible to read values from it without writing _G in front. This is no longer the case." (from the wiki)
  • ZombieSpy
    ZombieSpy over 9 years
    then you can call the function even without specifying _G not anymore