Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make vehicle autodespawn after some time if nobody is using it? (closed)

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

3 answers

✓ Accepted answer

A VehicleSeat already has the property "Occupant", so this is how I'd go about doing it:

local car = script.Parent 
local seat = script.Parent.VehicleSeat
-- change the car and seat variables if they are different in your case.

seat.Changed:connect(function()
	if not seat.Occupant then
		wait(20)
		car:Destroy()
	end
end)

2 comments

0

Your code worked! Just changed some things to better suit my needs (See edit above). TYVM!

0

No problem! :) Mind accepting my answer by clicking the tick?

try this its simple

script.Parent.ChildRemoved:connect(function() --this is a trigger which is essentially checking to see if you jump from the seat--
		wait(20) --this line tells the code to wait twenty second
		script.Parent.Parent:remove() --this line deletes the car based on model location--
	end)

2 comments

0

It won't do for me since it will only work after you get out of the vehicle, but I had in mind that vehicle would be removed even when it's just spawned in, but isn't used.

0

By the way if another player would get in the vehicle it would despawn anyway because after waiting 20s it wouldn't check if the vehicle wasn't occupied again and would just remove it. So maybe what about a method for checking if vehicle is used or not.

you need to make 2 scripts, each in the seat of the car:

local seat=script.Parent
local occupant=true
local trigger=seat:WaitForChild'Occupant'
local cancel=trigger:WaitForChild'Cancel'
while true do
	if seat.Occupant and occupant==false then
		occupant=true
		cancel.Value=true
	elseif not seat.Occupant and occupant==true then
		occupant=false
		trigger:Fire(20)--chnage 20 to the amount of time you want to wait until removal
	end
	wait()
end

and

local seat=script.Parent
local car=seat.Parent--change the location of car to match your car
local trigger=Instance.new('BindableEvent',seat)
local cancel=Instance.new('BoolValue',trigger)
trigger.Event:connect(function(countdown)
	for i=1,countdown do
		wait(1)
		if cancel.Value==true then
			cancel.Value=false
			return
		end
	end
	car:Destroy()
end)
trigger.Name='Occupant'
cancel.Name='Cancel'

This discussion is preserved as it was. New answers, comments, and votes are closed.