How to Make A Model Detect TOuch

I didn’t name my parts… so very inconvenient. Any ideas? It’s for a badge

How to Make A Model Detect TOuch

Like, for a thing to trigger every time a Player comes in contact with one of the Parts inside the Model?

Yes, but every single part in the model
By the way, I loved get a drink at 3am

local Model = workspace.yourModel

for _,Part:Part in Model:GetChildren() do
	Part.Touched:Connect(function(partThatTouched)
		print(`Part {Part} from model {Model} was touched by {partThatTouched}`)
	end)
end

Is this what you wanted to do?

2 Likes

Yes, but there are models inside of the model, what should I do about that/

You would use :GetDescendants() on the Model instead, since that would look through everything contained within that Model to the very last layer.

However, depending on what you are trying to achieve with this, connecting a Touched event to every part within the model could be very intensive, so keep in mind that there may be other, more performant solutions for what you’re trying to create.

local Model = workspace.yourModel

for _,Part:Part in Model:GetDescendants() do
	if not Part:IsA("Part") then continue end -- Makes sure we're only selecting parts
	Part.Touched:Connect(function(partThatTouched)
		print(`Part {Part} from model {Model} was touched by {partThatTouched}`)
	end)
end

This code should work.

This is true, and @Cookie_Byte should probably rethink how they’re going to go about this.

3 Likes

If the model happens to have a humanoid you can use Humanoid.Touched which automatically includes all of the parts in it (though I doubt your model has a Humanoid)

1 Like

I have a quick question since I am not familiar with this; what does the :Part do in that section of the loop? Not sure if it was a typo or if it’s something related to typechecking that I don’t know much about.

1 Like

It’s typechecking. Very useful for autocomplete and bugproofing your code.

2 Likes

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