I have two scripts in the same location and was wondering if there was a way for one script to call a function from another.
You can use module scripts, like mentioned by V3N0M_Z, or even bindables. You can also use _G
or shared
, but I highly discourage these.
Module scripts are similar to scripts and local scripts but they will not run unless you call require
on it and they must return no less than and no more than one value. Note that modules will only ever run once, and if you attempt to require the same module script more than once, the result of require
on this module will be cached. In this example I will return a function from this module script.
-- # Module Script A local function printHelloWorld() print("Hello World") end return printHelloWorld
-- # Script B local printHelloWorld = require(game:GetService("ReplicatedStorage").ModuleScript) -- if your printHelloWorld()
If you need to return multiple functions you can just return a table with all your functions in it
If you are familiar with remote events and remote functions, bindables are similar, but they are for communication on the same machine. That is, server to server or client to client. These do not cross the client-server boundary.
-- # Script A bindableEvent.Event:Connect(function(...) print(...) end) bindableFunction.OnInvoke = function(tbl, item) --[[ """ This function checks if table 'tbl' contains item 'item'. If tbl contains the item, return true. Otherwise, return false. """ --]] for _, v in ipairs(tbl) do if v == item then return true end end return false end
-- # Script B bindableEvent:Fire("Hello World", 2, true) local containsItem = bindableFunction:Invoke({"one", "two", "three"}, "one") print(containsItem)
[As mentioned previously in another thread by BlackJPI] Roblox has four different objects that allow for cross script communication, there is some great documentation on some of these, but I will try to do my best to break them down for you. Bindable Events
A BindableEvent is an object that has two important cross communication properties. The first of the two is
:Fire(...).
This function will trigger the BindableEvent. This is then detected by the next important property,
.Event.
It is important to note that these can only communicate between scripts of the same scope (server to server or client to client)
First Script
BindableEvent:Fire(argument)
Second Script
BindableEvent.Event:connect(function(argument) --code end)
Bindable Function
A BindableFunction is very similar to a BindableEvent, however it allows you to return a value back to the script that Invoked the function. One script will use the function :Invoke(...)
in order to trigger the BindableFunction. The other script will detect this with it's event .OnInvoke
, however something must be returned.
It is important to note that these can only communicate between scripts of the same scope (server to server or client to client)
First Script
local something = BindableFunction:Invoke(argument)
Second Script
BindableFunction.OnInvoke:connect(function(argument) --code return something end)
Remote Event
A RemoteEvent is very similar to a BindableEvent except it allows for cross scope communication (server to client or client to server). If you want to trigger an event on the server from a client script, you would use the function :FireServer(...).
If you want to trigger an event on the client from a server script you would use :FireAllClients(...)
or if you want it to only fire for a specific client you would use:FireClient(player, ...)
. The event is received on the server with.OnServerEvent
(note that an argument of player is automatically sent) or on the client with .OnClientEvent
.
Server Script
RemoteEvent:FireClient(player, argument)
Client Script
RemoteEvent.OnClientEvent:connect(function(argument) --code end)
Remote Function
A RemoteFunction is very similar to a RemoteEvent but, like a BindableFunction, allow a value to be returned to the triggering script. To trigger the server you use the function :InvokeServer(...)
and to trigger the client you use :InvokeClient(player, ...)
(note there is no InvokeAllClients function). The function is received on the server with .OnServerInvoke
(note that an argument of player is automatically sent) and on the client with .OnClientInvoke
. Remember that something must be returned.
Client Script
local something = RemoteFunction:InvokeServer(argument)
Server Script
RemoteFunction.OnServerInvoke:connect(function(player, argument) --code return something end)
If your script requires the same function to be used in different scripts, see 'ModuleScripts'.
There are multiple methods for that.
1 - ModuleScripts Functions inside ModuleScripts can be required from any script (Including both Server and Client). For more information, please read Click Me.
2 - Global Variables This is quite complicated so I'll not explain it. Read the Developer API yourself. Global Functions
3 - BindableEvent/BindableFunction They work similar to RemoteEvent and RemoteFunction. But they are for ServerScript to ServerScript communication. For more information, click me for BindableEvent page, click me for BindableFunction page.
Hopefully this helped!