How can I make multiple parts use the Part.Touched function to link to one function?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Title.
  2. What is the issue? Include screenshots / videos if possible!
    It fails, just saying me these two things in the output:
    Capture
    The weird thing, I do have a model called “The Maze (Easy)”. It spawns just when the script is activated. When I don’t use the WaitForChild function, it just says it doesn’t exist.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Here’s the script for the connections (for the music changing script):
connection1 = script.Parent.Parent:WaitForChild("The Maze (Easy)").Spawns.Part1.Touched:Connect(ChangeMusic)
connection2 = script.Parent.Parent:WaitForChild("The Maze (Easy)").Spawns.Part2.Touched:Connect(ChangeMusic)
connection3 = script.Parent.Parent:WaitForChild("The Maze (Easy)").Spawns.Part3.Touched:Connect(ChangeMusic)
connection4 = script.Parent.Parent:WaitForChild("The Maze (Easy)").Spawns.Part4.Touched:Connect(ChangeMusic)

Can you verify a few things?

  1. The model has the same capitalizations as your code
  2. There’s no extra spaces before or after the name of the model
  3. The model is actually going into Workspace
  4. If the script is a Script (not LocalScript), is the model being parented to Workspace also a Script, or is it a LocalScript?
  5. Can you share screenshots of your Workspace hierarchy that contains the model?
1 Like

Im on mobile so expect some bugs. Also im NOT 100% sure.

    local touchpartsmodel = --group every part you want to use in 1 function
    for i,v in pairs(touchpartsmodel:GetChildren()) do
        if v:isA("BasePart") then
            v.Touched:connect(functon(hit)
                --code here
             end)
         end
     end

why does my code look like that lol oh nvm i fixed it

It’s a local script:
Here’s the hierarchy of the workspace (after a 1 minute):

Umm, I think I used this same method in an earlier version of my script:

local children = script.Parent.Parent:WaitForChild("The Maze (Easy)").Spawns:GetChildren()

for i, v in pairs(children) do
--since everything in that directory is a part, I don't need to specify whether if it's a basepart...
v.Touched:Connect(ChangeMusic)
end

Dont forget to check v! If its not a BasePart then you will probably have errors.

EVERYTHING in that directory is only parts. I will show you a hierachy or that folder:
image

From your video it looks like the name of the model is “The Maze (Eazy)” and in your code it is “The Maze (Easy)”. Your spelling is not consistent.

2 Likes

@xZylter Oh, sorry, I didn’t know that because the two had similar names. All I had to do is change the name and now, it works, thank you! :smiley:

1 Like