How to detect vehicleseat occupant with a wait?

The player sits down, gets out of the vehicle seat, if for 5 minutes no one sits on this seat, vehicle:Destroy()

How can i do that ?
i already tried the
task.wai(…)
if vehicleSeat.Occupant then …

but it doesn’t work

You can make use of delayed threads:

--!strict

local vehicleSeat: VehicleSeat = script.Parent
local destroyThread: thread? = nil

local DESTROY_TIME: number = 5

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function(): ()
	if vehicleSeat.Occupant ~= nil and destroyThread ~= nil then
		print("cancelled")
		task.cancel(destroyThread)
		destroyThread = nil
	elseif vehicleSeat.Occupant == nil and destroyThread == nil then
		print("destroying")
		destroyThread = task.delay(DESTROY_TIME, function(): ()
			destroyThread = nil
			vehicleSeat:Destroy()
		end)
	end
end)
3 Likes

I haven’t had time to do it yet, but I think it will be perfect! THANKS !