How to make two scripts run at the same time?

I wanted to make two scripts run at the same time. I have a script that is going to pick a random tower/obby and another timer script. Those scripts are separated so I added a wait(480) command on the random tower script and the timer is 8 minutes.

However, most of the time the random tower script runs first and so, it will regenerate the tower when the timer is still on 0:12 (or less or more). Are there any ways to fix this problem?

Will appreciate it if you know how to.

3 Likes

You could always create two different scripts but one of them have a wait and when the wait finished the script runs. Also I would say you should add two waits on both of the scripts and it should stop your problems.

Either use BindableEvents and fire them whenever you need something to happen in the other script, or put them in the same script and use coroutines (I recommend this option)

You could use a RemoteEvent, for example, if one script starts, then that script will fire an event, if that event is fired, then the other script will get that event and run the script.

Here’s the article on the Developer Hub if you wish to use coroutines or just want to take a look at them before deciding what you would like to do.

Use coroutines or spawns.

spawn(function()
   -- code
end)

RemoteEvents are for Client to Server or Server to Client communication. BindableEvents would be appropriate for this scenario because I am assuming he is talking about two Server Scripts.

You should for sure use coroutine. It’s the easiest way, and does not require the hassle of having 2 different scripts.

You can use spawn, but I discourage it due to there being a delay before the function runs and the delay can vary depending on lag. Therefore, I would recommend using coroutines as they do not have a delay.

1 Like

I know, it was merely an option. I prefer coroutines personally since they run without a delay but they are harder to debug.

I would also advise using coroutines because they offer some extra functionality.

Coroutines somehow don’t work with the scripts.

How don’t the coroutines work in a script.

If you could provide a code sample it would help everyone better understand what you mean

Have a look at the following article and confirm to me that you are sure your code makes sense and you don’t know why it isn’t working.
https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine

You could coritine the function in 1 script and call the objects using your set methouds.

Okay so like

Example:
Script 1:

print(“Hello World”)

Script 2:

print(“Hello Earth”)

How do I make the coroutines?

1 Like