What do you want to achieve? Keep it simple and clear!
I was learning how to script, and i came across this little issue that i couldn’t fix.
What is the issue? Include screenshots / videos if possible!
The issue is in the title, but here it is again:
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to look for solutions but the only one i found had no solutions, and wasn’t written correctly.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
I don’t want to get the full script fixed, but rather tell me what’s the issue, so i can learn and understand from this error. Here is my function and how it’s being called:
local function fade(Part)
if not isTouched and GameOnValue.Value == true then
isTouched = true
for count = 1, 10 do
Part.Transparency = count / 10
task.wait(0.1)
end
Part.CanCollide = false
task.wait(3)
Part.CanCollide = true
Part.Transparency = 0
isTouched = false
end
end
for fadingParts in ipairs(FadingPartsFolder:GetChildren()) do
fadingParts.Touched:Connect(fade)
end
With iterating in Luau, all iterators (except your own custom ones) will return the key and the value. It looks like you’ve got an array, but only one iteration variable is set, and it’s assigned to the first return value - a number. The instance itself is discarded.
Try this:
--_ placeholder for index
for _, fadingParts in FadingPartsFolder:GetChildren() do --no iterator - Luau will choose the best iterator
fadingParts.Touched:Connect(fade)
end
right now your fadingParts variable represents the key going through the array (FadingPartsFolder:GetChildren())
for fadingParts in ipairs(FadingPartsFolder:GetChildren()) do
-- since :GetChildren() returns an array (ipairs also converts to an array)
-- fadingParts is always a number
fadingParts.Touched:Connect(fade)
end
something like what @12345koip said would be better
for key, fadingParts in FadingPartsFolder:GetChildren() do
-- key would be a number, fadingParts would be some kind of part
fadingParts.Touched:Connect(fade)
end
True, never though about that. I’ve used pairs my entire life and never had any issue. But not using them would work better, just like you said. But the only thing i put was _, so i guess that’s not such a big deal.
But you’re completely true!
this is only true for small arrays and the difference is negligible. the idea that pairs or ipairs should still be used in any circumstances outside of avoiding the penance of your own mistakes in creating a table is just a community myth. go and test the iterators on increasingly sizeable set-key tables and arrays and you (as i did and others) will find general iteration is the winner over both, and doesn’t discriminate against if its set-key or not.
well the generalized iterator is not “normal”; its not even in actual lua, its just in roblox’s luau, as pairs, next or ipairs is otherwise required unless youre doing a numerical for loop. not tryna rn but yeah
yes im aware, i wasnt talking about the numerical for loop, im talking about key-pair iterators, thus why i was confused about your response that you were … also talking about it?
pairs is the “older” iterator which will iterate over anything - all it does it return the next iterator, the table to traverse, and a starting key of nil.
ipairs is intended for use on smaller arrays. It stops when it hits a non-consecutive number index, and won’t iterate at all if not started at key 1. This is because non-consecutive key arrays or ones that don’t start at 1 are treated as dictionaries in Luau.
Luau’s generalised iteration is the best option, during compile time (luau → bytecode), it will select the best iterator depending on the expected size and elements of the given table; if it determines ipairs to be the most efficient, it will use ipairs. This prevents unneccessary overhead at runtime. It also allows a custom iterator to be used through the __iter metamethod.