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

How do I make this script spawn different cars, not a different car when there is a new server?

So, I have this script that spawns in random AI cars. Except it only spawns a different car per server. I want it to spawn a different car each time a car is spawned. How do I do that?

	local Cars = {5083437873, 5083676767}

		whichNum = math.random(1, #Cars)
		
while wait(1) do
	wait(math.random(1,1))
	print(("Picked: " .. 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.Body.Drive.Disabled = false
	root.Parent = workspace
	root:MoveTo(Vector3.new(math.random(1,38), 2.999, (math.random(-10000,-2.6))))
end

1 comment

0

Do you mean each server, the kind of car spawns the same and than different as the server time goes on?

1 answer

✓ Accepted answer

It only picks one car per server because the whichNum value isn't inside the loop. It has to be inside the loop for it to pick something different each time.

Also, I changed some things up, such as your use of:

:Remove() instead of :Destroy() (:Remove() is deprecated and shouldn't be used).

math.random instead of Random.new() (Random.new() is optional but way better).

local random = Random.new()
local Cars = {5083437873, 5083676767}

while wait(1) do
	wait(random:NextInteger(1,1)) --why 1,1?
	whichNum = random:NextInteger(1, #Cars)
	print(("Picked: " .. whichNum))
	
	local root = game:GetService("InsertService"):LoadAsset(Cars[whichNum])

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

	root = root:GetChildren()
	if #root == 1 then
		root = root[1]
	else return end
	print(root)
	root.Body.Drive.Disabled = false
	root.Parent = workspace
	root:MoveTo(Vector3.new(random:NextInteger(1,38), 2.999, (random:NextInteger(-10000,-2.6))))
end

1 comment

1

1,1 is a placeholder,

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