Correct, well, this is assuming you understand that the y
in r[y]
is an element of table t
, which happens to the Sound
object that we stored earlier as you can see in the below loop:
for x,y in pairs(t) do
if y:IsA("Sound") then
y.Volume = r[y] * volume.Value
end
end
The same loop can be rewritten as:
for index, sound in pairs(t) do
if sound:IsA("Sound") then
sound.Volume = r[sound] * volume.Value
end
end
In programming, this is why it can be incredibly helpful to name variables appropriately and meaningfully instead of single alphabets, because in more complicated or lengthier codes, it becomes crucial you can clearly understand exactly what variable means what.
And that’s half true. Yes you would run into an error, but not for trying to multiply an Instance with a number, because r[x]
wouldn’t be an Instance, it would be nil
. Why? Because r[x]
would return nil
since x
is an integer (in table t
), not an object and we already replaced all direct indices of the r
table with sound instances originally, hence you can no longer call a value from the r
table directly through an index number, such as r[3]
.
Here, let me simplify. For example, if you have a table Table
:
local Table = {
"A",
"B",
"C"
}
This table is the exact same table as:
local Table = {
[1] = "A",
[2] = "B",
[3] = "C"
}
So if you were to get the second index of the Table using print(Table[2])
, it will return "B"
.
Now, let’s rewrite the same table but with different indices.
local Table = {
["X"] = "A",
["Y"] = "B",
["C"] = "C"
}
Now if you try to call the same print(Table[2])
, it’ll return nil
instead of "B"
because we’ve overwritten the defaultly generated index values with characters, or strings.
This is exactly what we did with your code but instead of strings, it’s objects.
I hope that makes more sense. Cheers.