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

How do I check if any part in a model is touched?

I have a model named Car. I am attempting to check if any part in Car is touched, and if so, run a function that damages the car, etc. However, I cannot get this to work. I believe I could use GetChildren and check if each one is a part, but would this be laggy? Using the Touched event directly on Car did not work:

car.Touched:connect(collide)

Apparently I cannot use the Touched event on a model. This is understandable.

So, how would I do this without causing lag, having glitchy hitboxes, or some similar problem?

Also, how do I check if the part I collided with is part of the model?

4 comments

0

Making a table with 2000+ values is not laggy, so why would 100 values or so be laggy?

0

So it's not laggy. But how would I check for the Touched event with every part in the table?

0

You might be better off adding in the touched event to the part that is doing the damage

0

kingdom5, that would not work because I am having other cars running into cars so I would have the same problem anyway.

1 answer

✓ Accepted answer

I understand that you don't want the script to be laggy and looping through the parts will not cause lag. Initially it is just doing a normal index key pairs loop and all you have to do is check if the part is a part and if it is then just connect the touched event. In no way is this inefficient or bad.

for _, Part in pairs(Model:GetChildren()) do
	if Part:IsA('Part') then
		Part.Touched:connect(function()
			print(Part.Name .. ' was touched.')
		end)
	end
end

1 comment

0

Yes, this will work. Thank you.

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