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

for i,v in pairs won't work. What's wrong?

Asked by 5 years ago
Edited 5 years ago

I trying to make the fires light up in order. "Fires" is a model with 14 parts named 1 to 14. even named to 1,14 it doesn't make it in order

for i,b in ipairs(copy.Fires:GetChildren()) do
local fire = b.Middle.Fire
fire.Enabled = true
wait(1)
end)

I thought using "ipairs" would work. but still ignoring the order, why?

0
Here is an answer about looping with iterators that I wrote a while back: https://scriptinghelpers.org/questions/69456/how-does-an-advanced-for-loop-actually-work User#25115 0 — 5y
0
wow, thanks! iagometroid 61 — 5y

2 answers

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

Sometimes the children are added in a shuffled order. If your children are named from 1 to 14, why not use a generic for loop with an integer?

for i=1,14 do
    copy.Fires:FindFirstChild(tostring(i)).Middle.Fire.Enabled = true
    wait(1)
end
0
i didnt think to do that way... Thank you! iagometroid 61 — 5y
Ad
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

You can use for i=1,(ammount) ... end. but i maked a simple script for detect fires, and delay

You can see of For loop in roblox wiki: For - Wiki

Here is the script:


-- Name the parts with 1, 2, 3, 4 ... local copy = script.Parent.Copy -- Location of folder local v = 0 -- Do not change for i=1,#copy.Fires:GetChildren() do -- For loop to make a delay v = v + 1 -- Add 1 for the value if copy.Fires[v] ~= nil then -- if item not nil if copy.Fires[v].Middle:FindFirstChild("Fire") then -- If find fire if copy.Fires[v].Middle.Fire.Enabled == false then -- If fire not enabled copy.Fires[v].Middle.Fire.Enabled = true -- Enable fire end end else break; -- End script. end wait(1) -- Delay time end

Or you can simple use:


-- Name the parts with 1, 2, 3, 4 ... local copy = script.Parent.Copy for i=1,#copy.Fires:GetChildren() do if copy.Fires:FindFirstChild(tostring(i)) then if copy.Fires[i].Middle.Fire.Enabled == false then copy.Fires[i].Middle.Fire.Enabled = true end end wait(1) end

Why did I make this code difficult instead of using [i]? for me I tried with [i] and was not in any way in order so I decided to do this.

Hope it helped :D

Answer this question