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

How would i make a car delete itself if a player doesn't get into it within 20 seconds?

Asked by 4 years ago
seat = script.Parent
s = script


function onChildAdded(child)
    if child.Name == "SeatWeld" then
        local human = child.part1.Parent:findFirstChild("Humanoid") 
        if (human ~= nil) then
            print("ln")

        end
    end
end

function onChildRemoved(child)
    if (child.Name == "SeatWeld") then  
        local human = child.part1.Parent:findFirstChild("Humanoid") 
        if (human ~= nil) then
            print("Out")
            wait(20)
            script.Parent.Parent:Destroy()
        end
    end
end


script.Parent.ChildAdded:connect(onChildAdded)
script.Parent.ChildRemoved:connect(onChildRemoved)

Ok, thanks for reading and hopefully answering! So anyways, when a player gets into a car and gets out, the car will delete it self with his code:

if (human ~= nil) then
            print("Human OUT")
            script.Parent.Parent:Destroy() -- this one
        end

but im trying to use a ultimate driving type car spawner, but of course people sometimes dont go into the cars So how would i make a car delete itself within 20 seconds if nobody goes into it? Thanks!

2 answers

Log in to vote
1
Answered by 4 years ago

I found this code here, I think it can help you

local Timeout = 20 --How long the vehicle can be empty before it despawns

local function timedOut()
    for i=1, Timeout do
        wait(1)
        if vehicleSeat.Occupant then
            return
        end
    end
    return true
end

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if vehicleSeat.Occupant == nil and timedOut() then
        vehicle:Destroy()
    end
end)

Make sure to change vehicleSeat to the seat of the car so the code works.

Ad
Log in to vote
0
Answered by 4 years ago

You may try this:

local seat = script.Parent

wait(20)

if not script.Parent.ChildAdded then
    script.Parent.Parent:Destroy()
end

if script.Parent.ChildAdded then
    return
end

Answer this question