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

How could i make a blinking light that doesnt affect the timing of the script it is running in?

Asked by
IrishFix 124
5 years ago

I need to find a way to make a light that blinks Really Black to orange every so often, but if i do it in the script, it will pause the script to run it. How do i get around this?

3 answers

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

If I understand your question correctly, I'm assuming you want to have a function that will change the colour of a part to simulate a blinking light if you want it to not affect the timing of that script you're going to want to run it in a different thread so you can continue on with normal tasks.

There are 2 ways you can do this.

Coroutines

Spawn

Both of them do different things. I recommend reading them both to try to accustom it to your idea. Good luck!

  • Best Regards, -Syn
0
Thank you! I will read up on this right now, thanks for your help! IrishFix 124 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

There is a built in function that Roblox adds called spawn. It creates a new thread that runs separately from the other code in the script.

Basic usage would look like

spawn(function()
    while true do
        wait()
        print(5)
    end
end)

while true do
    wait()
    print(6)
end

This would constantly print out 5 and 6 together.

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

Instead of creating new threads as other answers suggest, you should instead put the code after your light blinking code before your light blinking code, such as turning

blinkLights()
doStuff()

to

doStuff()
blinkLights()

You should avoid creating threads that you can avoid so the thread manager has less threads to handle. If you have another infinite loop afterwards such as

while true do
    wait(1)
    doStuff()
end
while true do
    wait(1)
    doOtherStuff()
end

you can combine them to one loop, like so

while true do
    wait(1)
    doStuff()
    doOtherStuff()
end

If they have different wait times, you can turn something like

while true do
    wait(1)
    doStuff()
end
while true do
    wait(3)
    doOtherStuff()
end

to

local iteration = 1
while true do
    doStuff()
    if iteration == 3 then
        doOtherStuff()
        iteration = 1
    else
        iteration = iteration + 1
    end
    wait(1)
end

This will help your code avoid creating a lot of unneeded threads, creating a more performant script and cleaner code.

Answer this question