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.
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?"
_G
in front. This is no longer the case." (from the wiki) –
Supercool 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.
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()
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!
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
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.
then you can call the function even without specifying _G
not anymore –
Supercool You could use Module Scripts which were thankfully added. You could put the functions in there, then call and use them anywhere else!
© 2022 - 2024 — McMap. All rights reserved.