Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Communicating from Script to Script???

I have a script called "End" in a model in workspace. I have a script called "Generate" in server script service. How can I have these two scripts "communicate" with each other so that I can integrate it into my onTouched event in the end script?

2 answers

You could use a BindableEvent, which has the ability for scripts to talk to one another upon event. Here's an example.

local Bindable = Location_Of_Bindable.BindableEvent;

wait(1);

Bindable:Fire("Hello World!") -- Fires the bindable with the argument

And, within a second script.

local Bindable = Location_Of_Bindable.BindableEvent;

Bindable.Event:Connect(function(Str) -- Listens to be fired
	print("Message sent:", Str); -- Prints what was given when executed
end)

The second code should print Message sent: Hello World!

Stuff talked about

  1. BindableEvent

Stuff related, but not talked about 2. BindableFunction - Basically uses a function for when called upon. Think of these as remote, but for their respective sides. (Server-To-Server, LocalScript-To-LocalScript (same client only though))

Apologies if this wasn't "high quality", I haven't post an answer in months. Regardless, I hope this helped. :)

If you have any questions, please don't hesitate to ask. :D

5 comments

0

happy first-post-in-months post

0

bro, events are bitly harder than require or _g, but you want rep too :\

0

Don't make idiotic comments, gloveshun.

0

^

0

That's awful and sad. :/ You can't post answers w/o someone thinking that's "they're gonna get rep for it".

use _G like: script1:

_G.variable = "Hi"

script2:

print(_G.variable)

OUTPUT: "Hi"

[EDIT] or Modulescript:

local box = {
["Variable1"] = "Var1",
["Variable2"] = "Var2";
}
return box

script:

local Variable_box = require(game.Workspace.ModuleScript)
function printall()
for i, v in pairs(Variable_box) do
	print(v)
end
end


--function
	printall()

output: Var1 Var2

3 comments

0

i use _G oftentimes

0

G is slow. Module scripts slow you to keep track of variables/ functions between scripts

0

and if i not want to use _G, i use require()

This discussion is preserved as it was. New answers, comments, and votes are closed.