I tried doing what @rtvr56565 suggested about putting in a script into each of the parts, which also works, but I was wondering what the code for loops in my situation might look like.
It is possible, it just that don’t switch their names. In this case, we are gonna use a loop called for loop. What this loop will do is that it first checks if the name is that name, if it is, we will connect that part to that event along with the function.
thank you, I’ll check this out with my scripting partner and see if we can learn more from this lol
So if I use the for loop, then only the part touched will have the event fired regardless of whether or not a different part has the same name?
Yes, even though you have multiple parts named as the same, they are different. They have a unique identifier.
Okay that makes a lot of sense. I’ll learn for loops next now. Thanks for your help
That dude’s solution is what I was talking about. Try using it.
I’ll try and assist in this regard.
For loops, they’re a mighty fine tool and very useful, the most common built-in method is what I will go over.
Method: pairs
Summary: When you call pairs
with a table
as the only parameter, it will return an iterator
function. This function is used with the specific syntax of the for
loop in lua, but can be used in other ways irrelevant to this subject.
Example:
local OurTargetInstance = workspace.TargetInstance -- this is the instance we will iterate the children of.
-- Now, let's write our loop.
for Index, Object in pairs(OurTargetInstance:GetChildren()) do
-- Alrighty, so we have our functional for loop here.
-- As you may notice, it has three key points. "Index" which is the numerical..
-- .."Index" of the "Object" which is the returned instance.
-- Next, "in pairs(...) do" defines the "Iterator" of the loop. And marks the start..
-- ..of the loop's scope (the code to do on each loop.) An iterator function looks..
-- ..something like this: ..
-- ..function () UpValueIndex += 1; return UpValueArray[UpValueIndex] end
-- But enough of that... Let's make our code so something!
-- Firstly let's define some stuff.
local ObjectType,ObjectNumber,ObjectName = Object.ClassName, Index, Object.Name;
print(ObjectType, ObjectNumber, ObjectName) -- And let's print it to the output!
-- As you go, you will see the ObjectNumber increase.
-- When you loop through anything's children, they will be arranged in an order..
-- ..of numbers, therefore making naming completely irrelevant, unless you aren't..
-- ..storing them anywhere, and plan to use them further on in the script.
-- Since this does not cache their specific positions, due to them being the same..
-- ..name, it may randomly swap some of them for different Indexes. If you need..
-- ..to use them later on, I'd suggest predefining a table and storing them with..
-- .. the following code: YourTable[Index] = Object
end -- And finally, we will close the scope so the script doesn't error.
Whew. That’s a lot of explanation, and might’ve overdone the sentence splitting. But I think it should be helpful in some fashion. If you have any questions please do ask. I’m not always the fastest responder but I’ll get to it.
WOAH thank you, this helped a lot must have taken a while to explain all that so thoroughly. appreciate it