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

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

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 comment

1

Yes, use BindableEvents.

1 answer

✓ Accepted answer

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.

2 comments

0

How does bindableevents differ from remoteevents and remote functions?

0

Remoteevents and remote functions are for Client <-> Server communication, while bindableevents are just server to server

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