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:
01 | while true do |
02 | wait( 20 ) -- after 20s check if vehicle is occupied or not |
03 | local seat = script.Parent.VehicleSeat |
04 | local occupied = false -- set occupied to false to erase before checked positive outcome |
05 | if seat:FindFirstChild( "SeatWeld" ) then -- check if vehicle is occupied by detecting vehicleseats seatweld; if positive, then mark as occupied |
06 | occupied = true |
07 | end |
08 | if occupied = = false then |
09 | script.Parent:Destroy() -- destroy the vehicle if not marked as occupied |
10 | end |
11 | 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.
1 | local seat = script.Parent |
2 | local vehicle = seat.Parent |
3 |
4 | while true do -- infinite loop instead of a function since i want it to constantly check the seat |
5 | wait( 10 ) -- changed 20s to 10s (just changed my mind about the check rate) |
6 | if not seat.Occupant then |
7 | vehicle:Destroy() |
8 | end |
9 | end |
A VehicleSeat already has the property "Occupant", so this is how I'd go about doing it:
01 | local car = script.Parent |
02 | local seat = script.Parent.VehicleSeat |
03 | -- change the car and seat variables if they are different in your case. |
04 |
05 | seat.Changed:connect( function () |
06 | if not seat.Occupant then |
07 | wait( 20 ) |
08 | car:Destroy() |
09 | end |
10 | end ) |
try this its simple
1 | script.Parent.ChildRemoved:connect( function () --this is a trigger which is essentially checking to see if you jump from the seat-- |
2 | wait( 20 ) --this line tells the code to wait twenty second |
3 | script.Parent.Parent:remove() --this line deletes the car based on model location-- |
4 | end ) |
you need to make 2 scripts, each in the seat of the car:
01 | local seat = script.Parent |
02 | local occupant = true |
03 | local trigger = seat:WaitForChild 'Occupant' |
04 | local cancel = trigger:WaitForChild 'Cancel' |
05 | while true do |
06 | if seat.Occupant and occupant = = false then |
07 | occupant = true |
08 | cancel.Value = true |
09 | elseif not seat.Occupant and occupant = = true then |
10 | occupant = false |
11 | trigger:Fire( 20 ) --chnage 20 to the amount of time you want to wait until removal |
12 | end |
13 | wait() |
14 | end |
and
01 | local seat = script.Parent |
02 | local car = seat.Parent --change the location of car to match your car |
03 | local trigger = Instance.new( 'BindableEvent' ,seat) |
04 | local cancel = Instance.new( 'BoolValue' ,trigger) |
05 | trigger.Event:connect( function (countdown) |
06 | for i = 1 ,countdown do |
07 | wait( 1 ) |
08 | if cancel.Value = = true then |
09 | cancel.Value = false |
10 | return |
11 | end |
12 | end |
13 | car:Destroy() |
14 | end ) |
15 | trigger.Name = 'Occupant' |
16 | cancel.Name = 'Cancel' |