Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Remove car if no one is in it for 30 seconds?

Hi, I am trying to create a script where if no one is in the car for at least 30 seconds the car gets removed. I have tried creating different scripts but they never end up working and I need a bit of help, here is one script:



function onTouch(seat) 
	local player = seat.Parent:FindFirstChild("Humanoid") 
	if (player ~= nil) then	
	print("No del")
	elseif (player == nil) then 
		print("del")
	wait(30)
	script.Parent.Parent.Parent:Destroy()
end	
end 


script.Parent.Touched:connect(onTouch)



So I made this one that if there is no humanoid touching the seat it gets removed but it does not work and I am confused and do not know why.

I lost the other one I made but it had something to do with this

if script.Parent.Occupant == nil then
wait(30)
script.Parent.Parent.Parent:Destroy()
elseif script.Parent.Occupant ~= nil then
print("Human in car")
end

This last one was big and confusing but it worked, it put the value into the local player and when you'd click it the value would change to true or false. But afterwards I tried adding in another script to destroy the car if the value was false. The script below was in the gui

people = false

script.Parent.MouseButton1Click:connect(function()
	print("does")
	if game.Players.LocalPlayer:FindFirstChild("Value") == nil then
	Instance.new("BoolValue").Parent = game.Players.LocalPlayer
	game.Players.LocalPlayer.Value.Value = true 
	else
	if people == false then
		game.Players.LocalPlayer.Value.Value = false
		people = true
	elseif people == true then
		game.Players.LocalPlayer.Value.Value = true 
		people = false 
			if game.Players.LocalPlayer.Value.Value == true then 
	print("parked")
end
	
	end
	end
end)

this script here was in the vehicle seat. Separate script in vehicle seat.

if game.Players.LocalPlayer.Value.Value == false then
wait(30)
script.Parent.Parent.Parent.Destroy()
elseif game.Players.LocalPlayer.Value.Value == true then
print("In car")

None of these scripts worked and I do not know why, I need help with understanding this please. I tried changing some "elseif" to just "else" Thank you very much for your time. Probably an easy mistake, after all it is 12:44AM here

2 answers

Something like this might work if you have the heirarchy as

>CarModel
>>Script
>>Seat
local timer = 30
script.Parent.Seat.Changed:Connect(function(prop)
	if prop == "Occupant" then
		timer = 30
	end
end)
while wait(1) do
	if not script.Parent.Seat.Occupant then
		timer = timer - 1
		if timer <= 0 then
			script.Parent:Destroy()
		end
	end
end

3 comments

0

That will destroy the car after 30 seconds, even if a player is still in it (which just adds more time when they first get in). The player would have to keep hopping in and out to keep the car.

0

Would this edited version work?

0

Yeah I think so

This should do the trick. Make sure to change vehicle and vehicleSeat if needed.

local TIMEOUT = 30 -- How long the timer runs before removal
local vehicle = script.Parent -- Change this if needed
local vehicleSeat = script.Parent.VehicleSeat -- Change this if needed

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.