Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there any way to have two scripts running and interact with each other at the same time?

Asked by 5 years ago

I am working on a game and I don't need help with any script specifically but I was hoping to get help on an issue I have encountered. I have a script that runs to close certain parts of the map, and has a lot of waits and random sequences. This works fine when I run it, but I want there to be the option for someone to win the game (by being the last one alive) even if the zone isn't fully finished running yet. I also need the zone to start at a point in the main script that will have the player win. I don't have the winner code written yet because I don't know how to incorporate it with the rest of the loop. I hope this is specific enough I know I put quite a bit into this but maybe someone can help :).

1
Yes, use BindableEvents. awesomeipod 607 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

BindableEvents.

Let's say we have Script A and Script B, both loaded in completely different places in the gamespace.

First, we'll put a BindableEvent in ReplicatedStorage. This is how the two scripts will interact.

!ReplicatedStorage -> Events -> GiveANumber

Script A looks like this:

while wait(1) do
    game:GetService("ReplicatedStorage").Events.GiveANumber:Fire(math.random(1,5))
end

and Script B looks like this:

game:GetService("ReplicatedStorage").Events.GiveANumber.Event:Connect(function(arg)
    print("Wow, Script A just gave me a "..arg.."! Thanks, Script A!")
end)

If we run this, Script A will send a random number between 1 and 5 to the BindableEvent, which will deliver the number to Script B which will output it.

EDIT: If you want to communicate between the server and client or vice-versa, use RemoteEvents. BindableEvents will not work in this situation.

0
How does bindableevents differ from remoteevents and remote functions? xxXTimeXxx 101 — 5y
0
Remoteevents and remote functions are for Client <-> Server communication, while bindableevents are just server to server masterblokz 58 — 5y
Ad

Answer this question