How do I start a function with any part with the same name

  1. What do you want to achieve?
    I want a function to trigger anytime the player touches a part but it also triggers if the player touches another part with the same name

  2. What is the issue?
    it only triggers if I touch one of the parts

  3. What solutions have you tried so far?
    I tried to use findfirstchild but it still only works for one part

Im doing this because I basically want people to use an attack so I just dupe it, and the code is already like 400 lines so I want to keep it short. the part has 6 hitboxes, but I can just use the model’s name (I think).

u can get a workaround by looping through model which contain hit parts

for _, part in Model:GetChildren() do
   if part.Name == The_Name_You_Want then
         part.Touched   --ur hit function
   end
end
1 Like

You can write the function(not as an anonymous function) and then connect it to all the parts you want. Use a for loop to check for every part with the same name.

local function onTouchTrigger(touched)
    -- your function here
end

for _, instance in Model:GetChildren() do
    if part.Name == "triggerBrickOrSomething" then
        part.Touched:Connect(onTouchTrigger)
    end
end

The more fancier alternative is to use CollectionService.

after a couple of adjustments, it worked, mine was:

for _, part in game.Workspace:GetChildren() do
	if part.Name == beam then
		part.hi.Touched:Connect(apllywind)
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.