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

How to loop two things at the same time?

What I mean by loop is "for i do" What I'm having a problem with is that I want to have two loops active at the same time, but what really happens is that the first loop works then when that's finished, the second loop activates and finishes. What I want to do is that both loops happen at the same time. I mean, I could just make a separate script for the second loop, but I like to keep things organized. Anyway, here's the script:


local gui = script.Parent
local frame = gui.Frame
local soul = frame.Soul
local blur = game.Lighting.Blur
local viggy = frame.Vignette
wait(3)

soul:TweenPosition(UDim2.new(0.5, -100, 0.5, -200), "InOut", 4) 

for i = 0, 20, 1 do
	blur.Size = i
	wait(0.01)
	
end

for i = 1, 0 , -0.1 do
	viggy.ImageTransparency = i
    wait(0.01)
end



1 answer

✓ Accepted answer

There are a few ways to do this, but the easiest is to use the spawn function.

Here's how you use it:

function Func1()
	-- code
end

spawn(Func1)

spawn takes a function as an argument (the function being what it will run in a different thread).


In your context, the code would be something like this:

spawn(function() -- put the shortest loop in the spawn so when the longest one finishes, they are both guaranteed to be done
	for i = 1, 0, -0.1 do
		viggy.ImageTransparency = i
		wait(0.01)
	end
end)

for i = 0, 20, 1 do
	blur.Size = i
	wait(0.01)
end

TIP: Use Tabs and not Spaces to format your code. It is much cleaner and more effective.

Hope I helped!

~TDP

2 comments

0

Hey, thanks it works. I'll remember this next time I work with loops.

0

No problem!

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