Need help with Car spawner

Hello dev forum, I am once again here looking for help.Currently I’m making a car spawner which looks for free parking lots to spawn the car in so there won’t be any cars stuck in other cars which works but it just dosn’t do anything if it find it blocked, Is there a way to make it look for a free lot and try again instead of not spawning a car?

for i,v in pairs(script.Parent.Buttons:GetChildren()) do
		if game.ServerStorage.Vehicles.Zivil[v.Name] then
		if not game.Workspace:FindFirstChild(script.Parent.Parent.Parent.Name.."'s Car") then
			local wk = game.Workspace.Game.CarSpawner.Zivil:GetChildren()
			local rndm = wk[math.random(1, #wk)]
			print("Lot's available: "..#wk.." Selected: "..rndm.Name)
			if #rndm:GetTouchingParts() <= 0 then
				local clone = game.ServerStorage.Vehicles.Zivil[v.Name]:Clone()
				clone.Name = script.Parent.Parent.Parent.Name.."'s Car"
				clone.Parent = game.Workspace
				print("Lot Blocked")
1 Like

have a bool variable in each parking space and use region3’s to check if there is a vehicle in the region.

1 Like

why would I do that if the detection works?

1 Like

Oh I got the message messed up. You can try making a function that checks all spaces if they are empty using for i, v in ipairs(lotsfolder) to get each region3 make a individual hitbox and use this function:

function PartToRegion3(part)
	local abs = math.abs

	local cf = obj.CFrame -- this causes a LuaBridge invocation + heap allocation to create CFrame object - expensive! - but no way around it. we need the cframe
	local size = obj.Size -- this causes a LuaBridge invocation + heap allocation to create Vector3 object - expensive! - but no way around it
	local sx, sy, sz = size.X, size.Y, size.Z -- this causes 3 Lua->C++ invocations

	local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:components() -- this causes 1 Lua->C++ invocations and gets all components of cframe in one go, with no allocations

	local wsx = 0.5 * (abs(R00) * sx + abs(R01) * sy + abs(R02) * sz) -- this requires 3 Lua->C++ invocations to call abs, but no hash lookups since we cached abs value above; otherwise this is just a bunch of local ops
	local wsy = 0.5 * (abs(R10) * sx + abs(R11) * sy + abs(R12) * sz) -- same
	local wsz = 0.5 * (abs(R20) * sx + abs(R21) * sy + abs(R22) * sz) -- same
	-- just a bunch of local ops
	local minx = x - wsx
	local miny = y - wsy
	local minz = z - wsz

	local maxx = x + wsx
	local maxy = y + wsy
	local maxz = z + wsz
	local minv, maxv =, miny, minz),, maxy, maxz)
	return, maxv)

After looping through each lot spot if there is a free spot then return the Lot’s hitbox and you can spawn the car in the Lot. (The reason to turn it into a function is so when you do return it stops looping.)



Hey, I just found something way more simple while scripting something else, you just have to recall the function like this for example:

local function spawn()
local c = spawner:GetChildren()
if #c == 0 then
copy.Parent = Workspace