Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect when multiple objects are touched?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.