While scripting, i have came across "trying to index number with "Touched""

  1. 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.

  1. What is the issue? Include screenshots / videos if possible!

The issue is in the title, but here it is again:
Capture d’écran 2025-04-20 à 22.11.27

  1. 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

i just wanted to help make it clear

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

you could ‘ignore’ the key by using an underscore

2 Likes

Try

for _, fadingParts in pairs(FadingPartsFolder:GetChildren()) do
	fadingParts.Touched:Connect(fade)
end

You used fadingParts like it was a number, since for uses i and v (i,v)
For example,

for i,v ..

You used it like:

for fadingParts,v

And I recommend you to not use ipairs, use pairs

i thought you didnt need to use “Ipairs” anymore

You can still use them. But it’s better using pairs because Ipairs works with tables that have no gaps, in case of GetChildren it’s better using pairs

well ipairs would be for arrays while pairs would be for dictionaries
:GetChildren() returns an array (you should use ipairs over pairs in this case)

yet, i’d recommend you guys never use ipairs or pairs (again, like @12345koip said)

1 Like

Random unrelated question: Which one is theoretically faster?

I’m assuming it depends on its input. I’d take a look at how it is structured but I just thought of this question as I was reading this thread lol.

i know it’s slower to use ipairs and pairs but i don’t remember which one is faster

it definitely depends on the input, but i would assume ipairs is faster

Okay, yeah, I thought ipairs might be slower as I thought (possibly incorrectly) that the input would be sorted first.

I’m also assuming the difference is potentially trivial for game dev. needs, but I’ll take a closer look in a sec haha.

1 Like

ipairs is faster for arrays. It starts at the index of 1 and ends when it encounters a nil value.

1 Like

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!

1 Like

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.

I’m only talking about the iterators. A normal for loop is gonna be faster than both, ofc.

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 :nerd: rn but yeah

i meant normal for loop as in the numeric for loop

for i = 1, 10 do
    -- bruh
end

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?

oh, ok

idek anymore

Just to clear this up:

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.

3 Likes

You only needed to change the ipairs to pairs