My target was to create a script that destroys the vehicle after like 20 seconds if nobody is sitting in the vehicle seat. This is what i came up with:
while true do
wait(20) -- after 20s check if vehicle is occupied or not
local seat = script.Parent.VehicleSeat
local occupied = false -- set occupied to false to erase before checked positive outcome
if seat:FindFirstChild("SeatWeld") then -- check if vehicle is occupied by detecting vehicleseats seatweld; if positive, then mark as occupied
occupied = true
end
if occupied == false then
script.Parent:Destroy() -- destroy the vehicle if not marked as occupied
end
end
Basically if vehicle is not occupied it doesn't despawn after 20 seconds. The script is inside the vehicle (vehicle parts group). Could it be because the script contains another infinite loop above this current infinite loop? Thank you for any help and excuse me for any mistakes in spelling, grammar etc. - english is not my valid language.
Edit:
I found a working way of doing it! TYVM Omarstein! All I did is just modified a bit your code and created a new script inside the VehicleSeat.
local seat = script.Parent
local vehicle = seat.Parent
while true do -- infinite loop instead of a function since i want it to constantly check the seat
wait(10) -- changed 20s to 10s (just changed my mind about the check rate)
if not seat.Occupant then
vehicle:Destroy()
end
end

2 comments
Your code worked! Just changed some things to better suit my needs (See edit above). TYVM!
No problem! :) Mind accepting my answer by clicking the tick?