How to run code if something doesn't exist

I was making a one car only vehicle spawner script and was wondering how you run code if something doesn’t exist

system = script.Parent

model = system.Station --

backup = model:Clone()

regen = system.Regen



function checkRegen()
	if script.plr.Value == nil then
		if regen.Value == 1 then

			model:remove()

			wait(1)

			model = backup:Clone()

			model.Parent = system

			model:MakeJoints()

		end
	end
	

end

regen.Changed:connect(checkRegen)

Code: The one that changes the value

button = script.Parent

regen = button.Parent.Regen



local debounce = false



function onTouch(hit)

	local h = hit.Parent:FindFirstChild("Humanoid")

	if h ~= nil and debounce == false then
		script.Parent.Parent.Script.plr.Value = hit.Parent.Vehicle

		debounce = true

		button.BrickColor = BrickColor.Black()

		regen.Value = 1

		wait(1)

		regen.Value = 0

		wait(5)

		button.BrickColor = BrickColor.new(104)

		debounce = false

	end

end

button.Touched:connect(onTouch)

I’m a bit confused on what you mean? Would you be able to give more details? Also you don’t need to have spaces after ever line of code.

You can use FindFirstChild() then do:

if model:FindFirstChild("instance") then
    print("instance of model found")
else
    print("instance of model not found")

its not completely my code, its for a neighborhood of robloxia v.4 recreation and im assisting one of the devs

This would work but once the vehicle spawns it always is going to be found

and isn’t it what you wanted? to check if the car exist, if not then execute other code

Can you be more specific so I can assist you normally.

so to be specific I abandoned all that code since it’s meant to work like it worked in V.4 which it deletes it when the vehicle seat is touched and also makes it so much less labor has to be done to convert existing vehicle spawners by just putting the code in the seat.

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		if hit.Parent:FindFirstChild("Vehicle") then
			script.Parent:Destroy()
		end
	end
end)

Is there a better way to do this?

1 Like

I understood it, Yet I don’t understand what you’re trying to fulfill.

So, when someone spawns a vehicle I want it to make sure they don’t have 2.
So if they have no other vehicles it allows them in the seat, if they have 2 vehicles it deletes the car.
The vehicle gets put in the workspace plr model btw

pcall(function()
      --Code
end)

Or check if the thing isn’t equal to nil.