What are you attempting to achieve?
After 10 seconds, if a player isn’t in a VehicleSeat of a car, the car despawns. Else, if a player is in a VehicleSeat of the car, the car doesn’t despawn. What is the issue?
The issue is that even if a player is in a car, the car still despawns, as seen in this clip:
What solutions have you tried so far?
None so far.
Code
local Car = script.Parent
local VehicleSeat = script.Parent.VehicleSeat
if VehicleSeat.Occupant == nil then
print("Occupant is nil")
wait(10)
Car:Destroy()
Car:BreakJoints()
print("Car despawned")
elseif VehicleSeat.Occupant == "Humanoid" then
print("Humanoid is in VehicleSeat")
end
The reason why your car is despawning is because when your code above is called, obviously the vehicles occupant is going to be nil as the player isn’t in the car yet. The car then waits 10 seconds and then despawns, even if the player then gets in.
I would recommend making a for loop that checks if the player is in the seat every iteration. If the player isn’t, continue looping until it reaches 10, then despawn the car. If the player is, wait until the player is out of the car, then restart the for loop.
local Car = script.Parent
local VehicleSeat = script.Parent.VehicleSeat
VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
local start = tick()
while not VehicleSeat.Occupant do
wait()
if tick() - start >= 10 then
Car:Destroy()
Car:BreakJoints() -- lol why would you do this?
break
end
end
end)