When it comes to connecting with scripts and either breaching that client-server (server - client) or breaching that script-script interaction. There are events that you can actually use. These events/functions are called bindable and remote. I’ll be going though what they do and how they’re handy. If you wanted a simpler way to connect scripts. I’d recommend using modules and wrapping them around shared or _G
Basic Information
RBXScriptSignal
An RBXScriptSignal is basically a signal that someone can connect to. This signal when fired will run every function within their connections.
An example of an RBXScriptSignal would be something like: game.Changed, game.ChildAdded, workspace.ChildRemoved
To connect to these ScriptSignals you’d call the method :Connect on the signal. Therefore connecting you to that signal. There is also another feature that these ScriptSignals give to you. They have another method called :Wait. when you call this, the current thread will pause/yield until that event is fired.
RBXScriptSignal
Events
An event is a simple RBXScriptSignal. But instead it has a method called :Fire, This allows you to manually fire this event with custom arguments. Etc.
Handling Events
Functions
This is very much the same as an event. But instead you’d have to call Invoke. The reason behind this is because this Function will return something. You can only assign one function to these. This is because you can’t return more than one object.
Functions
My model example
bindables
These are used for communicating script - script. Instead of communicating over the server-client. These instances don’t offer this.
Bindable Event
Now this is basically what I said above. But this is an instance that will talk between two different scripts. I’ll be going though some code examples to clear up some things. I’ll be demonstrating the :Connect feature and the :Wait feature of an Bindable Event. I’ll also go though the :Fire Feature too.
Connect - Managing connections
local Event = script.Parent.Event
Event.Event:Connect(function(TextFromScript_1)
print(TextFromScript_1)
end)
Yield - Yielding until an event
local Event = script.Parent.Event
Event.Event:Wait()
print("Waited!")
Firing - Firing the event
-- // This is my first script. Which will fire the event.
local Event = script.Parent.Event -- // Defining our event
wait(.5) -- // So script_2 can connect.
Event:Fire("Hello from Script_1") -- // send a cool text over the event.
Bindable Function
Again. This is what I said above. I’ll just go though some code examples and you hopefully should be able to pick up on some things.
Invoke - Invoking the function to return something
-- // This is my first script. Which will fire the event.
local Function = script.Parent.Function -- // Defining our function
wait(.5) -- // So script_2 can connect.
local Sum = Function:Invoke(5, 5) -- // This will return (5 + 5); Calcuated from the script_2
print(Sum) -- // Printing our sum!
OnInvoke - Connecting to the invoke
-- // This is my second script. Which will capture the function.
local Function = script.Parent.Function -- // Defining our function
Function.OnInvoke = function(Number1, Number2) -- // Assigning the function to the invoke.
return (Number1 + Number2) -- // returning the sum.
end -- // end of function.
remotes
These are used for communicating to client and to server. These are used in basically all games ever since Filtering Enabled was forced.
The whole remote system is quite a lot like the bindable system. So bear in mind. These are basically going to be about the same scripts.
Remote Event
FireClient - Single fireing to a client.
local Event = script.Parent.Event
Event:FireClient(Client, "Etc")
FireAllClients - Firing to all clients in the server
local Event = script.Parent.Event
Event:FireAllClients("Etc")
Connect - Connection to the event
This may get a little big confusing. But i’ll be going though differences on the server and on the client.
-- // Client
local Event = script.Parent.Event
Event.OnClientEvent:Connect(function(Etc)
end)
The server gets a bonus argument, This argument is the player that actually fired the remote.
-- // Server
local Event = script.Parent.Event
Event.OnServerEvent:Connect(function(Player, Etc)
end)
Yielding - Waiting for the event.
-- // Client
local Event = script.Parent.Event
Event.OnClientEvent:Wait()
-- // Server
local Event = script.Parent.Event
Event.OnServerEvent:Wait()
Remote Function
Invoking
local Function = script.Parent.Function
Function:Invoke(Client, "Etc..")
OnInvoke - Connecting to the invoke
local Function = script.Parent.Function
Function.OnInvoke = function()
return "Sent from Script_2"
end
my model
Model Example.rbxm (2.6 KB)
By the way. This is just off my general knowledge, Sorry if anyone finds anything wrong/different and feel free to correct me!
This also took me like half an hour because i’m very distracted. So sorry if there is any spelling mistakes.