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 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
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
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