How can I make a script talk between eachother, kind of Java classes. I want to send to another script what button is selected by just sending it a string. Could I do like
selected = game.StartGUI.ScreenGUI.Frame.Script.buttonSelected()
How can I make a script talk between eachother, kind of Java classes. I want to send to another script what button is selected by just sending it a string. Could I do like
selected = game.StartGUI.ScreenGUI.Frame.Script.buttonSelected()
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)
This is an awesome reply, thank you. It helped me a lot.
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
If you want "like" scripts to communicate with each other, consider using http://wiki.roblox.com/index.php?title=API:Class/BindableEvent or http://wiki.roblox.com/index.php?title=API:Class/BindableFunction (if you want to return a value). "Like" meaning client-to-client or server-to-server.
If you want "unlike" scripts to communicate with each other, consider using http://wiki.roblox.com/index.php?title=RemoteFunction_and_RemoteEvent_Tutorial (server-to-client or vice versa).
If you want to just import an implementation of a bunch of functions, you can use ModuleScripts http://wiki.roblox.com/index.php/ModuleScript