My for i,v in pairs script does 1 part at a time and is very slow

while true do
wait(0.2)
for i,v in pairs(game.Workspace.flammableparts:GetDescendants()) do
if v:IsA(“BasePart”) then
wait(0.1)
local isfirethere = v:FindFirstChildWhichIsA(“Fire”)
if isfirethere then
else
wait(0.2)
local fire = game.ReplicatedStorage.particles.Fire:Clone()
fire.Parent = v
v.Anchored = false
v.BrickColor = BrickColor.new(“Black”)
wait(4.5)
local onfire = v:WaitForChild(“Fire”)
if onfire then
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.9
wait(0.1)
v:Destroy()
end
end
end
if v:IsA(“MeshPart”) then
local isfirethere = v:FindFirstChildWhichIsA(“Fire”)
if isfirethere then
else
v.Anchored = false
local fire = game.ReplicatedStorage.particles.Fire:Clone()
fire.Parent = v
v.BrickColor = BrickColor.new(“Black”)
v.Texture = (“”)
local onfire = v:WaitForChild(“Fire”)
if onfire then
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.9
wait(0.1)
v:Destroy()
end
end
end
if v:IsA(“UnionOperation”) then
local isfirethere = v:FindFirstChildWhichIsA(“Fire”)
if isfirethere then
else
v.Anchored = false
local fire = game.ReplicatedStorage.particles.Fire:Clone()
fire.Parent = v
v.BrickColor = BrickColor.new(“Black”)
v.UsePartColor = true
local onfire = v:WaitForChild(“Fire”)
if onfire then
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.9
wait(0.1)
v:Destroy()
end
end
end
end
end

1 Like

It’s slow because of the wait(x) that you put everywhere.

“for i, v in pairs”…

In Luau, ipairs works really fast, contrary to what most online resources say about ipairs in vanilla Lua, use it for numerically indexed tables where key-value iteration is unnecessary, although this will only improve the speed by negligible difference.

The main reason is because you’re yielding too much (wait(x))

So how would i speed this up while keeping a wait(4.5) for the parts to burn

if you’re yielding on purpose, you can’t un-yield that?

just use ipairs for a very minor speed boost, instead of pairs.

What I mean is you’re explicitly waiting(4.5) or something, the code itself runs as fast as it can, just remove all the wait() s if you want it to go as fast as it can

Im pretty confused how do i just make my script go fast i am caveman

You can wrap all your code in a coroutine:

for i, v in pairs(something) do
    coroutine.wrap(function()
    end)()
    wait(x)
end

Basically what a coroutine does it creates another thread. Lua is single threaded, meaning that it has to wait for the previous line to pass to execute the next line. Coroutines essentially create another script inside of a script.

3 Likes

Yes but the thing i want is multiple parts to be on fire at once

Yes, but you have so many wait(x) lines, plus the wait(4.5) that waits for each part to burn for it’s 4.5 seconds, then burns the next Part.
Use @VegetationBush’s suggestion to loop through all the Parts and send them to a coroutine, where you can light them on fire and as soon as you light them move on to the next item and light it without waiting until it’s burnt to move to the next one.

Now how would i implement that into my script?

It’s tough to read through your script when you copy/paste it here. A screenshot would work better.
If all the Parts and Unions that are flammable are in the same model and are the only items in that model then get rid of the “if v:IsA(“BasePart”) then”, “if v:IsA(“MeshPart”) then” and “if v:IsA(“UnionOperation”) then” lines and compress all your code into one loop containing all the Descendants of that Model since they already contain Fire.
Use @VegetationBush’s script and put your information into it to get the i, v Descendants of the Model, then when you send it to the coroutine and use your:
if isfirethere then
else
v.Anchored = false
local fire = game.ReplicatedStorage.particles.Fire:Clone()
fire.Parent = v
v.BrickColor = BrickColor.new(“Black”)
v.Texture = (“”)
local onfire = v:WaitForChild(“Fire”)
if onfire then
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.9
wait(0.1)
v:Destroy()
end

loop on each Part.

1 Like

You could use ‘spawn(f)’ and some state for each part to make it work.

For example:

-- create and init all flame parts
local flameParts = {}
for k, v in pairs(game.Workspace.flammableparts:GetDescendants()) do
    local flamePart = {}
    flamePart.part = v
    flamePart.Animating = false
    table.insert(flameParts, flamePart)
end

while true do
    wait(0.2)
    for k,flamePart in pairs(flameParts) do
        if flamePart and not flamePart.Animating then
            spawn(function()
                flamePart.Animating = true
                -- run animating code here
                flamePart.Animating = false
            end)
        end
    end
end