Car Despawn Script

So I’m trying to create a despawn script for my slot game, but I don’t think that this is the best way to write this script.

Currently when you get out of the car a 30 second timer starts and once that 30 seconds is up it checks again to see if anyone is in the car, but there are some bugs and glitches with this option. Also the way in my game that you spawn in cars is via the admin command :ins (AssetID) so some scripts don’t work properly.

This is my current script.

local seat = script.Parent
local car = seat.Parent

seat:GetPropertyChangedSignal('Occupant'):Connect(function()
	if not seat.Occupant then 
		wait(30)
		if not seat.Occupant then 
			car:Destroy()
		end
    end
end)

I would love to see some better ways on doing this,
Thanks F31!

2 Likes

You are correct there could be glitches with this option.

The best solution I can think up of at this moment is to do this:

  1. Either create a table of all cars in the game, or create some kind of NumberValue object inside of the seat.
  2. Every time someone leaves the chair, set the NumberValue's value to tick()
  3. Have a script check every second (or more seconds if there are a lot of cars) to see if the seat is not occupied and if the NumberValue's value is older than 30 seconds (tick()-NumberValue.Value > 30.) If those conditions are true, destroy it.

There might be other solutions, but this one is pretty decent without causing those bugs you mentioned.

Good luck.