I am making this script for spawning AI cars for a highway. Except it doesn't work. I get Argument 1 missing or nil. I don't get it. It shows Picked: nil

	local Cars = {5083437873, 5083437873}
	
	local numLoops = math.random(0,math.floor(1000*time()))
	numLoops = numLoops - (math.floor(numLoops / 10000)*10000)
	for i = 1,numLoops do
		whichNum = math.random(1,#Cars)
	end
while wait(1) do
	wait(math.random(1,1))
	print("Picked: ")
	print(whichNum)
	local root = game:GetService("InsertService"):LoadAsset(Cars[whichNum])

	local instances = root:GetChildren()
	if #instances == 0 then
		root:Remove()
		return
	end

	root = root:GetChildren()
	if #root == 1 then
		root = root[1]
	else return end
	print(root)
	root:MoveTo(-0.5, 2.997, math.random(-5,1000))
	root.Parent = workspace
end
5083437873, 5083437873

are placeholders because I haven't made more than 1 car yet

4 comments

0

What's going on with numLoops, what's the point of that?

0

I took it from an old random gear picker. I have no idea what numLoops are.

0

I'm pretty sure it's safe to get rid of that part, and just keep whichNum = math.random(1, #Cars)

0

Thanks, now it's working.