How can i make this loop generate more than just one car from the folder

i want to make a car spawner that generates them from a folder but as it is a server script when the cars generate, the “car” value is set to a different one, preventing the other cars from spawing

here is the code

for i,v in pairs(workspace:FindFirstChild("Vehicle"):WaitForChild("Spawns"):GetChildren()) do
	local Car = nil
	if game:GetService("ReplicatedStorage"):WaitForChild("Cars"):FindFirstChild(v.Name) then
		Car = game:GetService("ReplicatedStorage"):WaitForChild("Cars"):FindFirstChild(v.Name):Clone()
		Car.Parent = workspace.Vehicle.Cars
		Car:SetPrimaryPartCFrame(v.CFrame)
		
		while wait() do
			if (Car["Body Car"].Body.Position - v.Position).Magnitude > 15 then
				print("Over 15")
				Car = game:GetService("ReplicatedStorage"):WaitForChild("Cars"):FindFirstChild(v.Name):Clone()
				Car.Parent = workspace.Vehicle.Cars
				Car:SetPrimaryPartCFrame(v.CFrame)
			else
				print("Under 15")
			end
		end
	end
end

the folder layout is
Basic Car
Test
Multiple

In The Cars Folder In Replicated Storage All Of Those Are There

Just dont overwrite the Car variable. Make a new one called like CarClone.

that didnt work , it still only generates one car

Can you explain more what this code is supposed to do?

the code checks a folder in workspace that is called spawns inside there are parts which are called the same as a model in replicated storage (in the cars Folder), then it copies the car model and sets its cframe to the workspace parts cframe, then it checks if the car is 15 studs away then spawns a new car

So it has to do that for multiple spawns?

yes, it has to make the cars spawn for other spawn locations

You probably need this, you can start multiple loops in paralle with task.spawn

task.spawn(function()
	while wait() do
		...
	end
end

nope, still doesnt work, i put the loop in this script

Could you try this ?

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local VehicleFolder = workspace:WaitForChild("Vehicle", 30)
local SpawnsFolder = VehicleFolder and VehicleFolder:WaitForChild("Spawns", 30)
local CarsFolder = VehicleFolder and VehicleFolder:WaitForChild("Cars", 30)

local CarsStorage = ReplicatedStorage:WaitForChild("Cars", 30)

----------------------------------------------------------------------------------------

local function CreateNewCars()
	for i, Child in pairs(SpawnsFolder:GetChildren()) do
		local NewCar = CarsStorage:FindFirstChild(Child.Name)
		
		if NewCar then
			NewCar = NewCar:Clone()
			NewCar.Parent = CarsFolder
			NewCar:SetPrimaryPartCFrame(Child.CFrame)
		end
	end
end

local function UpdateCars()
	for i, Child in pairs(SpawnsFolder:GetChildren()) do
		local NewCar = CarsStorage:FindFirstChild(Child.Name)
		local Car = CarsFolder:FindFirstChild(Child.Name)
		
		if NewCar and Car and (Car["Body Car"].Body.Position - Child.Position).Magnitude > 15 then
			print("Over 15")
			NewCar = NewCar:Clone()
			NewCar.Parent = CarsFolder
			NewCar:SetPrimaryPartCFrame(Child.CFrame)
		else
			print("Under 15")
		end
	end
end

----------------------------------------------------------------------------------------

if SpawnsFolder and CarsFolder and CarsStorage then
	CreateNewCars()
	
	while task.wait(0.1)do
		UpdateCars()
	end
end

THANK YOU
This actually worked

nvm after testing the cars spawn once then spawn about 100

Alright, i found the problem, wait a minute i fix it ^^

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local VehicleFolder = workspace:WaitForChild("Vehicle", 30)
local SpawnsFolder = VehicleFolder and VehicleFolder:WaitForChild("Spawns", 30)
local CarsFolder = VehicleFolder and VehicleFolder:WaitForChild("Cars", 30)

local CarsStorage = ReplicatedStorage:WaitForChild("Cars", 30)

----------------------------------------------------------------------------------------

local function CreateNewCars()
	for i, Child in pairs(SpawnsFolder:GetChildren()) do
		local NewCar = CarsStorage:FindFirstChild(Child.Name)

		if NewCar then
			NewCar = NewCar:Clone()
			NewCar.Parent = CarsFolder
			NewCar:SetPrimaryPartCFrame(Child.CFrame)
		end
	end
end

local function GetCars(Child)
	local CanSpawn = true
	
	for i, Cars in pairs(CarsFolder:GetChildren()) do
		if Cars.Name == Child.Name and (Cars["Body Car"].Body.Position - Child.Position).Magnitude <= 15 then
			CanSpawn = false
		end
	end
	return CanSpawn
end

local function UpdateCars()
	for i, Child in pairs(SpawnsFolder:GetChildren()) do
		local NewCar = CarsStorage:FindFirstChild(Child.Name)
		local CanSpawn = GetCars(Child)

		if NewCar and CanSpawn == true then
			print("Over 15")
			NewCar = NewCar:Clone()
			NewCar.Parent = CarsFolder
			NewCar:SetPrimaryPartCFrame(Child.CFrame)
		else
			print("Under 15")
		end
	end
end

----------------------------------------------------------------------------------------

if SpawnsFolder and CarsFolder and CarsStorage then
	CreateNewCars()

	while task.wait(0.1)do
		UpdateCars()
	end
end
1 Like

THANKS IT WORKS :slight_smile:

1 Like

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