How could a script check if a part is not a part of a vehicle anymore?

I have a script almost working, but it doesn’t work while the vehicle is on the ground.
script: (in every block, it is also very laggy)

local ispes = false

function pes()
local g = script.Parent:GetChildren()
for i = 1, #g do
local touch = script.Parent.PrimaryPart.Touched:Connect(function() end)
local touching = script.Parent.PrimaryPart:GetTouchingParts()
if g[i]:IsA("WeldConstraint") then
	for i = 1, #touching do
		if (touching[i]:IsA("VehicleSeat") or touching[i].Parent:IsA("VehicleSeat")) and not touching[i]:IsDescendantOf(workspace.Bases) then
			script.Parent.IsSeat.Value = touching[i].Parent.Name
			ispes = false
		elseif not touching[i]:IsDescendantOf(script.Parent) and touching[i]:IsDescendantOf(workspace.PlacedObjects) and ispes == false then
			script.Parent.IsSeat.Value = touching[i].Parent.Parent.IsSeat.Value
		elseif touching[i].Name == "Terrain" and not touching[i]:IsDescendantOf(workspace.PlacedObjects) then
			script.Parent.IsSeat.Value = ""
			ispes = true 
		elseif (touching[i]:IsDescendantOf(workspace.PlacedObjects) and touching[i].Parent.Parent.IsSeat.Value == "") and ispes == true then
			script.Parent.IsSeat.Value = ""
		end
		end
	end
	if script.Parent.IsSeat.Value == "" and script.Parent:IsDescendantOf(workspace.PlacedObjects) then
		script.Parent.Parent = workspace.PlacedObjects
	else
		script.Parent.Parent = workspace.PlacedObjects[script.Parent.IsSeat.Value]
	end
	end
end

game:GetService("RunService").Heartbeat:Connect(pes)

the IsSeat Value is the seat that the blocks are attached to


at 0:22 i was un-anchoring the seat
at the end of it some blocks go outside the seat and the IsSeat value is set to nil.

1 Like

If all of the vehicle’s parts is in a model, you could probably use :GetDescendants() and iterate through it comparing the current part in the iteration with the part you’re trying to find.

If you are looking at the Parent/Ancestor, you can use FindFirstChild with the second parameter(recursive) to true

local someModel = vehicle:FindFirstChild("someName", true)
if someModel then
	...
end

If you are looking at the descendant, then just use FindFirstAncestor

local vehicle = somePart:FindFirstAncestor("someName")
if vehicle then
	...
end

ok. I worded the title wrong. But thanks for letting me know how to see if something is a part of something!

actuall problem:


I got a working system on if the blocks are a part of the vehicle. I dont know how to make the script know if it is not part of the vehicle anymore. Like, i move a block that is on the vehicle and the other blocks attached too it fall, but then it sees that it is touching the terrain and doesnt make it a part of the vehicle any more. But this is a problem because when i drive the vehicle, it thinks the parts are not part of the vehicle anymore.

I think you’re looking for the DescendantAdded and DescendantRemoving events, to see when a descendant has been added or removed

As he said, you can use those and to check if something is a descendant of a part by using :IsDescendantOf(part)
Hope this helped!

but when the part is detached, its still in the seat.
I need something to detect that it isnt part of it any more.

if part.Parent ~= otherPart then

wait hold on, can you send a better explanation of what you mean again?

like when I move one block, some others fall and those blocks still think they are part of the vehicle when they are not.

If there are joints connected, then, you could disconnect the joint (by making it’s Part0/Part1 nil), and check that

each block is welded by a weld constraint.

try to use part:GetTouchingParts()

Then there is your answer (unless you can’t change the Part0/Part1)


(the other 2 are just not in the image that are outside the seat)

hm, i’m not quite sure I know a solution for this. try part:GetJoints() or part:GetConnectedParts()

ok. I have never used those so how do i use them?

the wiki definitely will help if anything

part:GetConnectedParts() instead of part:GetTouchingParts() really helped. Thanks!

Sure! Make sure to mark my post as a solution too :smiley: