Vehicle despawn not working

I’m currently making a manual texted Vehicle despawn script. The problem is when the despawn starts and you get in the Vehicle again it would still despawn no matter if someone is in the Vehicle

local Vehicle = script.Parent.Parent.Parent.Jeep
local VehicleSeat = script.Parent
local TextLabel = script.Parent.BillboardGui.TextLabel

VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	while not VehicleSeat.Occupant do
		TextLabel.Text = "DESPAWNING IN 100"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 99"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 98"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 97"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 96"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 95"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 94"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 93"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 92"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 91"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 90"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 89"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 88"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 87"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 86"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 85"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 84"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 83"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 82"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 81"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 80"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 79"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 78"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 77"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 76"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 75"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 74"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 73"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 72"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 71"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 70"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 69"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 68"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 67"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 66"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 65"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 64"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 63"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 62"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 61"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 60"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 59"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 58"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 57"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 56"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 55"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 54"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 53"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 52"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 51"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 50"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 49"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 48"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 47"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 46"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 45"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 44"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 43"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 42"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 41"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 40"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 39"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 38"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 37"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 36"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 35"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 34"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 33"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 32"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 31"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 30"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 29"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 28"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 27"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 26"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 25"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 24"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 23"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 22"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 21"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 20"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 19"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 18"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 17"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 16"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 15"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 14"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 13"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 12"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 11"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 10"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 9"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 8"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 7"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 6"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 5"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 4"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 3"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 2"
		wait(1)
		TextLabel.Text = "DESPAWNING IN 1"
		wait(1)
		Vehicle:Destroy()
		break
	end
end)

I tried to use the else and elseif but still won’t work

local Vehicle = script.Parent.Parent.Parent.Jeep
local VehicleSeat = script.Parent
local TextLabel = script.Parent.BillboardGui.TextLabel

local idx = 100

VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	while not VehicleSeat.Occupant do
		TextLabel.Text = "DESPAWNING IN "..idx
		idx -= 1
		task.wait(1)
		if idx <= 0 then
			Vehicle:Destroy()
			break
		end
	end
end)

Works fine but the timer doesn’t reset

local Vehicle = script.Parent.Parent.Parent.Jeep
local VehicleSeat = script.Parent
local TextLabel = script.Parent.BillboardGui.TextLabel

local idx = 100

VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	while not VehicleSeat.Occupant do
		TextLabel.Text = "DESPAWNING IN "..idx
		idx -= 1
		task.wait(1)
		if idx <= 0 then
			Vehicle:Destroy()
			idx = 100
		end
	end
end)

Still the same, Timer doesn’t reset.

ofc, it only changes idx at last but not the value of the text label

local Vehicle = script.Parent.Parent.Parent.Jeep
local VehicleSeat = script.Parent
local TextLabel = script.Parent.BillboardGui.TextLabel


VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local despawnTime = 100
	
	-- Loop until despawnTime reaches 0
	while despawnTime > 0 do
		despawnTime -= game:GetService("RunService").Heartbeat:Wait() -- time passed since last frame
		
		if VehicleSeat.Occupant then 
			TextLabel.Text = "OCCUPIED"
			return 
		end
		
		TextLabel.Text = "DESPAWNING IN "..math.round(despawnTime)
		task.wait()
	end
	
	-- If it doesn't return after the loop, destroy
	Vehicle:Destroy()
end)

create a separate thread for the despawning part and you don’t need for the loop (sorry if i explained it wrong)

local Debris = game:GetService("Debris")

local Vehicle = script.Parent.Parent.Parent.Jeep
local VehicleSeat = script.Parent
local TextLabel = script.Parent.BillboardGui.TextLabel

local stringFormat = string.format

local despawnText = coroutine.create(function()
	for seconds = 100, 1, -1 do -- goes from 100 to 1
		TextLabel.Text = stringFormat("DESPAWNING IN %i SECONDS", seconds)
		task.wait(1)
	end
	
	Debris:AddItem(Vehicle, 0) -- safer way of destroying
end)

VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if VehicleSeat.Occupant ~= nil then -- if the seat is occupied
		-- cancelling/closing the thread
		task.cancel(despawnText)
		
		-- assigning the variable again after the previous thread is closed
		despawnText = coroutine.create(function()
			for seconds = 100, 1, -1 do
				TextLabel.Text = stringFormat("DESPAWNING IN %i SECONDS", seconds)
				task.wait(1)
			end
	
			Debris:AddItem(Vehicle, 0) -- safer way of destroying
		end)
		
		TextLabel.Text = ""
	else
		-- resuming/playing the thread
		task.spawn(despawnText)
	end
end)
local Vehicle = script.Parent.Parent.Parent.Jeep
local VehicleSeat = script.Parent
local TextLabel = script.Parent.BillboardGui.TextLabel

local idx = 100

VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	while not VehicleSeat.Occupant do
		TextLabel.Text = "DESPAWNING IN "..idx
		idx -= 1
		task.wait(1)
		if idx <= 0 then
			Vehicle:Destroy()
			break
		end
	end
	idx = 100
end)

This should work.

This is the neatest way to achieve what you’re looking for:

local Debris = game:GetService("Debris")

local VehicleSeat = script.Parent
local Vehicle = VehicleSeat.Parent.Parent.Jeep
local TextLabel = VehicleSeat.BillboardGui.TextLabel

VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if VehicleSeat.Occupant then return end

	for i = 100, 1, -1 do
		if VehicleSeat.Occupant then return end

		TextLabel.Text = "DESPAWNING IN "..i
		task.wait(1)
	end

	Debris:AddItem(Vehicle, 0)
end)

Tested it all and this one seems to be the one that fits perfectly and thanks to the others that helped

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.