Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make vehicle autodespawn after some time if nobody is using it? (closed)

Asked by
kazeks123 195
8 years ago
Edited 8 years ago

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:

01while 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
11end

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.

1local seat = script.Parent
2local vehicle = seat.Parent
3 
4while 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
9end

3 answers

Log in to vote
0
Answered by
Omarstein 100
8 years ago
Edited 8 years ago

A VehicleSeat already has the property "Occupant", so this is how I'd go about doing it:

01local car = script.Parent
02local seat = script.Parent.VehicleSeat
03-- change the car and seat variables if they are different in your case.
04 
05seat.Changed:connect(function()
06    if not seat.Occupant then
07        wait(20)
08        car:Destroy()
09    end
10end)
0
Your code worked! Just changed some things to better suit my needs (See edit above). TYVM! kazeks123 195 — 8y
0
No problem! :) Mind accepting my answer by clicking the tick? Omarstein 100 — 8y
Ad
Log in to vote
1
Answered by
sammiya1 134
8 years ago
Edited 8 years ago

try this its simple

1script.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)
0
It won't do for me since it will only work after you get out of the vehicle, but I had in mind that vehicle would be removed even when it's just spawned in, but isn't used. kazeks123 195 — 8y
0
By the way if another player would get in the vehicle it would despawn anyway because after waiting 20s it wouldn't check if the vehicle wasn't occupied again and would just remove it. So maybe what about a method for checking if vehicle is used or not. kazeks123 195 — 8y
Log in to vote
0
Answered by
Versimn 20
8 years ago
Edited 8 years ago

you need to make 2 scripts, each in the seat of the car:

01local seat=script.Parent
02local occupant=true
03local trigger=seat:WaitForChild'Occupant'
04local cancel=trigger:WaitForChild'Cancel'
05while 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()
14end

and

01local seat=script.Parent
02local car=seat.Parent--change the location of car to match your car
03local trigger=Instance.new('BindableEvent',seat)
04local cancel=Instance.new('BoolValue',trigger)
05trigger.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()
14end)
15trigger.Name='Occupant'
16cancel.Name='Cancel'

Answer this question