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

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

Asked by
nobo404 17
6 years ago
Edited 6 years ago

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?

0
Making a table with 2000+ values is not laggy, so why would 100 values or so be laggy? hiimgoodpack 2009 — 6y
0
So it's not laggy. But how would I check for the Touched event with every part in the table? nobo404 17 — 6y
0
You might be better off adding in the touched event to the part that is doing the damage User#5423 17 — 6y
0
kingdom5, that would not work because I am having other cars running into cars so I would have the same problem anyway. nobo404 17 — 6y

1 answer

Log in to vote
0
Answered by
AmWrath 41
6 years ago

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
0
Yes, this will work. Thank you. nobo404 17 — 6y
Ad

Answer this question