A single script to detect touch on any parts belonging to a model

Hello fellow developers,

I am making a border (so people cannot get past) for a zone in my game, and I have several parts that make up the border. All the border parts have been grouped together to form a model. However, I am struggling to find a way to have 1 script that can detect if anything is touching any of the parts in my model. I can easily insert a script into each part in the model to detect touch using the Touch event, but I feel this is not the most efficient way of doing it since I have many parts that form my border. I have explored the Roblox Developer pages by none seem to cover this aspect.

Any help would be greatly appreciated.

3 Likes

you can use this script:

for _,Part in ipairs(Model:GetDescendants()) do
      if Part:IsA("BasePart") then
        Part.Touched:Connect(function(Hit)
                   -- Do the code
           end)
    end
end

Note, if the group have many parts the connections will make lag.

16 Likes

hello? just a reminder to choose above as solution if that actually achieved what you want
or if it doesn’t, reply back

Why not just make a single part that represents your entire border and check that…?
You could try to encompass most of the border with tall invisible walls (physical and nonphysical) and that will be a lot easier to handle and improve performance.

4 Likes

Thank you BasedKnowledge and everyone for your help. Perfect!