While loop stops running for some reason

I created a car and I want it to spawn if it is not currently on the spawn point (so if a player drives it away, another player can come and get another car.)
but for some reason, after it spawns the car once, the whole script stops.

local spawner = script.Parent.PrimaryPart
local car = game.ReplicatedStorage.MailTruck
local carPresent = false
for i,v in pairs(spawner.Parent:GetChildren()) do
	if v:IsA("Part") or v:IsA("UnionOperation") then
		v.Transparency = 1
		v.CanCollide = false
	end
end

local function isCar(car)
	for i,v in pairs(spawner:GetTouchingParts()) do
		if v.Name == car.Name then 
			carPresent = false
		else
			carPresent = true
		end
	end

end
while wait() do
	for i = 0, 10, 1 do
		wait(1)
		isCar(car)
		print(carPresent)
		if carPresent == true then
			return
		elseif carPresent == false and i > 9 then
			local clone = car:Clone()
			clone.Parent = workspace
			clone.PrimaryPart.CFrame = spawner.CFrame
			carPresent = true
			return
		end
	end
end

It probably has something to do with the return part, but I am new to scripting so idk how to fix it.

I found an/the error, it it not detecting if it is touching the car it is only changing the carPresent variable by the script at the start and end

Ok I found my own solution, I created a new part that detects if it is touching. Thanks for viewing.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.