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
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)
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)
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'