How to make it so car won't destroy itself?

So I have a car in my game, which has crash physics, basically theres a script in some key parts that will :BreakJoints() (simulating a car crash), but, I ran into an issue, the car itself would touch it’s own parts and destroy itself, so I tried to implement a system for it to check two things:

  1. The thing it is hitting is NOT a player (humanoid)
  2. The thing it is hitting is NOT a descendant of itself.

Here is my code:

function onTouched(hit)
	local car = script.Parent.Parent.Parent
	if not hit.Parent:FindFirstChild([[Humanoid]]) and not hit:IsADescendantOf(car) then
			hit:BreakJoints()
		end
	end
connection = script.Parent.Touched:connect(onTouched)

So it fixes it, but it also makes it so it can’t destroy any other cars it touches outside of itself, and any time it touches a part, it gives the error: " IsADescendantOf is not a valid member of Part part it touched"

Can someone please help, thanks!

2 Likes

Still need help with this btw.

For some reason the function is called IsDescendantOf, not IsADescendantOf. Pretty weird, but that should solve the issue.

1 Like

Quite a common mistake, I’ve done it before myself. IsADescendantOf() isn’t a method of the class Instance. It’s IsDescendantOf(). So your code would look more like this

function onTouched(hit)
	local car = script.Parent.Parent.Parent
	if not hit.Parent:FindFirstChild([[Humanoid]]) and not hit:IsDescendantOf(car) then
			hit:BreakJoints()
		end
	end
connection = script.Parent.Touched:connect(onTouched)
2 Likes

Thank you both for your replies, it worked. Sorry I was afk for a bit but yeah, thanks to both of you

1 Like