Script2ScriptService v1
"The easier way to tell another script!"The Guide
You will need to insert the Module, which is linked above this text. Then put it on our ReplicatedStorage. Once that is finished, make any script, a local or server! But do know, we can’t make client to server or server to client communication, you need RemoteEvents
for that!
First let’s require the module, then we will need to create a handler, our ScriptEvent type thing.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local NewHandler = Script2ScriptService:CreateHandler("NewHandler")
Now that we created a new handler, let’s focus on our new script that listens when the handler is fired! Like last time, we require the module. Then we use :GetHandler()
this time. Inside of those parentheses, we put the handler we need to listen for. Remember this is cap sensitive.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local HandlerEvent = Script2ScriptService:GetHandler("NewHandler")
Now that we finished getting the handler, let’s do our function that listens for the handler. Let’s say, that the first script wants to change a block’s color by firing their handler. First we need to connect the HandlerEvent, like if it was a RBXScriptSignal. Inside of the functions, we can put our arguments.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local HandlerEvent = Script2ScriptService:GetHandler("NewHandler")
HandlerEvent:Connect(function(colorrequested)
end)
Let’s look back at our other script that created the handler.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local NewHandler = Script2ScriptService:CreateHandler("NewHandler")
To make the handler fire, we could just simply do NewHandler:Fire()
, then our arguments inside of the parentheses.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local NewHandler = Script2ScriptService:CreateHandler("NewHandler")
task.wait(1)
NewHandler:Fire(Color3.fromRGB(255, 0, 0))
NOTE: Sometimes, the script that fires as soon as when the game starts, will not be listened for yet. So make sure to add a wait() or task.wait() to make sure that doesn’t happen!
Now that is done, let’s go back to our other script and make it so that it sets the brick’s color as the color that was sent to it.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local HandlerEvent = Script2ScriptService:GetHandler("NewHandler")
HandlerEvent:Connect(function(colorrequested)
workspace.myCoolBrick.Color = colorrequested
end)
Let’s test it out!
Viola! It’s red now, and how did we do it? Through Script2ScriptService!
What's the difference?
You need to manually make a RemoteEvent, and another script-that is the opposite of the scripts that are trying to make a communication-to make the Script to Script communication. But with this, the service handles it for you in a simple way. By just simply doing CreateHandler()
! You didn’t even need to create a RemoteEvent and a script that you need to type to make the communication and such. You just simply did a function, and have another script do GetHandler()
!
Additional details
:GetHandler()
:GetHandler()
You can also make it send the handler without the event. Here is an example:
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local HandlerEvent = Script2ScriptService:GetHandler("TheHandler", true)
HandlerEvent:Fire("Hi!")
-- or
HandlerEvent.Event:Connect(thefunction)
You are also able to edit the amount of time to wait until the handler is created. But remember, that it will yield the script depending on how long you put it for.
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local HandlerEvent = Script2ScriptService:GetHandler("TheHandler", nil, math.huge)
HandlerEvent:Connect(function(whattosay)
print(whattosay)
end)
:RemoveHandler()
:RemoveHandler()
You can also get rid of a handler completely! Just simply do this:
local Script2ScriptService = require(game.ReplicatedStorage.Script2ScriptService)
local wasRemoved = Script2ScriptService:RemoveHandler("byebyehandler")
if wasRemoved then
print("it's gone!")
else
print("whoops, an error! nothing removed yet")
end
If you have any questions, let me know!