Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect when multiple objects are touched?

Asked by 2 years ago
Edited 2 years ago

How do I make it so that any part inside a folder does the exact same thing when touched.

For example, if one specific part inside a folder was touched I would use:

folder.part.Touched:Connect(
    function(touch)
        if touch.Parent:FindFirstChild("Humanoid") then
            print("part touched")
            Humanoid.Health = 0 
            wait(1)
        end
    end
)

But then I would have to rewrite this for every single part inside the folder, right?

So how would I avoid this?

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Here is code

function killing(touch)
    if touch.Parent:FindFirstChild("Humanoid") then
            print("part touched")
            Humanoid.Health = 0
            wait(1)
    end
end
for _, s in pairs(folder:GetChildren()) do
    s.Touched:Connect(killing)
end

I tested it, it having errors in console but this working good. P.S.: If you want avoid errors use pcall

Ad

Answer this question