Hello, I was wondering how I could make a "Touched" event that works for parts that have the same name. I tried to make a variable like this
bricks = workspace:FindFirstChild("spring")
thinking that the script would find all 3 children named spring, yet it only worked for one of the "spring" parts instead of the other two.
There are several ways to get around problems like this, all in which have their appropriate time for different implementations. Here I'll go over several methods and example for each one, and I'll let you decide which seems more suitable for your situation.
Yes, I know. This doesn't solve the core problem at all. However, what you name something is very important. You should chose names carefully and appropriately to make things easier to both visualize, and code.
Sometimes you can get around problems like this if you have a very specific location for each one of your objects.
Remember, the problem is trying to find an object with the same name, in the same directory. Using different directories (objects that store whatever you're searching for), can allow you to hold objects with the same name, so long as the directory is different.
This method however, is not practical if you're going to be scanning a large array of objects, all in which have the same name. That pretty much brings you back to your initial problem, since each directory would need a different name to access it's descendants. Take this for example, you can access both the parts that have the same name individually, but in different directories.
If you have a considerable amount of objects all with the same name, serving the same purpose, a for loop
is the guy for the job. All we're going to do, is simply collect all of the objects within a table using GetChildren
, then iterate that table with the for loop. During this process, we'll connect a Touched
event to each element inside the array as the loop cycles through. Here's an example:
-- We'll just say the parts are all in ServerStorage, to keep things neat. local storage = game:GetService("ServerStorage") -- Get all the objects inside ServerStorage local parts = storage:GetChildren() -- Initiate numeric for loop -- (For me, it's standard to use numeric for loops when working with true arrays, just because of their intended useage. But if you find using a generic for loop more appealing, you're more than free to do so) for i = 1, #parts do -- Get the index for the part within the GetChildren table local part = parts[i] -- A saftey precaution, in case a non-part object is -- inside our storage if part:IsA("BasePart") then -- Set the event on the individual part -- This is the event all the parts will be connected to part.Touched:connect(function(hit) print("Part was touched") end) end end
That sample code will connect a Touched event to all parts collected in the ServerStorage service. If you have any questions, or if anything was unclear, just let me know!
You can use a recursive function
This is an example only:-
local list = {} -- recursive function function search(itm) for i,v in pairs(itm:GetChildren()) do if v.Name == 'spring' then table.insert(list,v) end search(itm) -- recursive call end end search(game.Workspace) -- print table for i,v in pairs(list) do print(i,v) end
Pls comment if you don not fully understand this code.
Hope this help.