Log in to vote
0

# How to make certain parts of a loop repeat before other parts finish?

Asked by
manith513 121
4 months ago

I'm trying to make it where a house disappears part by part by increasing the transparency of the part, but I also want this affect to apply to up to 4 other parts on the house before they are finished.

local house = script.Parent
local runservice = game:GetService("RunService")

local debounce = 0

for i,v in pairs(house:GetChildren()) do
if v:IsA("Part") then
if debounce < 4 then
debounce = debounce + 1
for i = 1,10 do

v.Transparency = v.Transparency + 0.1

wait()

end
debounce = debounce - 1
end
end

end


I want the first four parts that the first for loop calls upon, to all start disappearing 0.1 seconds from each other, and I don't want more parts to start disappearing until the first one out of these four have disappeared and so on. I know in my code the loop does each step once before repeating so it wouldn't work but I'm not sure how to repeat certain parts before other parts have finished. Help would greatly be appreciated!

### 2 answers

Log in to vote
1
Answered by
NykoVania 231
4 months ago
Edited 4 months ago

I'm not sure if this helps, but if you use a repeat until loop you can wait until something is true or etc.

local a = false

repeat wait()
-- code here
until a == true

0
Yes! I used this for my debounce so that when the function was called on repeatedly with spawn, it would wait before progressing, thanks for your help. manith513 121 — 4mo
Log in to vote
0
Answered by
manith513 121
4 months ago

I figured it out, I used repeat wait() until thanks to Nyko and paired it with

spawn(function()

end)


which I didn't know about but let's you call on functions to start again without waiting for them to finish.

My finished code paired with this that worked for me:

local house = script.Parent
local runservice = game:GetService("RunService")

local debounce = 0

function mightwork(part)
repeat  wait() until debounce < 4
print("Started 1")
if debounce < 4 then
debounce = debounce + 1

for i = 1,25 do

part.Transparency = part.Transparency + 0.04

runservice.Heartbeat:Wait()

end
print("Ended 1")

debounce = debounce - 1
end

end

for i,v in pairs(house:GetChildren()) do
if v:IsA("Part") then
spawn(function()
mightwork(v)
end)

end
wait(0.15)
end


Thank you Nyko since your answers lead me to what I was looking for.