How to make a car spawn on custom part?

Hello, I want my car to clone after being clicked on the gui to a car spawn pad in the parking lot.

This is my current script how can I change it?

local plrCars = {}


game.ReplicatedStorage:WaitForChild("CarSpawnRE").OnServerEvent:Connect(function(plr, car)
	
	if car then
		
		if plrCars[plr] then
			
			plrCars[plr]:Destroy()
		end
		
		
		local newCar = car:Clone()	
		
		local hrp = plr.Character.HumanoidRootPart
		
		newCar:SetPrimaryPartCFrame(hrp.CFrame + (hrp.CFrame.LookVector * 10) + Vector3.new(0, 10, 0))
		
		newCar.Parent = workspace
		
		plrCars[plr] = newCar
	end
end)

Do you mean spawning it in the nearest parking lot?

I have a folder and I want it to randomly spawn in any of these.
image

I Have the move to done but how will i do it randomly?

local plrCars = {}

local pad = workspace.Pad    - CHANGE THIS
game.ReplicatedStorage:WaitForChild("CarSpawnRE").OnServerEvent:Connect(function(plr, car)
	
	if car then
		
		if plrCars[plr] then
			
			plrCars[plr]:Destroy()
		end
		
		
		local newCar = car:Clone()
		
		newCar:PivotTo(pad.CFrame + Vector3.new(0, 10, 0))
		
		newCar.Parent = workspace
		
		plrCars[plr] = newCar
	end
end)

Just change the pad variable so it will be your parking lot.

does this choose it randomly? or

Can you show your explorer so I’ll make a script?

image

Easy, Result is the random Parking Lot. (I guess the for i, v was unneccesary) (Yes it was)


    local RandomParkingLot = math.random(1, #game.Workspace.Spawn.CarSpawnLocations:GetChildren())

    local Result = game.Workspace.Spawn.CarSpawnLocations:GetChildren()[RandomParkingLot]

where would I put this in my script

Yes, then adjust the CFrame with “Result”

local plrCars = {}
local spawnLocations = workspace.AstralMap.Spawn.CarSpawnLocations
game.ReplicatedStorage:WaitForChild("CarSpawnRE").OnServerEvent:Connect(function(plr, car)
	
	if car then
		
		if plrCars[plr] then
			
			plrCars[plr]:Destroy()
		end
		
		local randomLot = spawnLocations:GetChildren()[math.random(1, #spawnLocations:GetChildren()) ]

		local newCar = car:Clone()
		
		newCar:PivotTo(randomLot.CFrame + Vector3.new(0, 10, 0))
		
		newCar.Parent = workspace
		
		plrCars[plr] = newCar
	end
end)
2 Likes

Tysm is it also possible to move the player to the car after it spawns?

Yes. Try this.

local plrCars = {}
local spawnLocations = workspace.AstralMap.Spawn.CarSpawnLocations
game.ReplicatedStorage:WaitForChild("CarSpawnRE").OnServerEvent:Connect(function(plr, car)
	
	if car then
		
		if plrCars[plr] then
			
			plrCars[plr]:Destroy()
		end
		
		local randomLot = spawnLocations:GetChildren()[math.random(1, #spawnLocations:GetChildren()) ]

		local newCar = car:Clone()
		
		newCar:PivotTo(randomLot.CFrame + Vector3.new(0, 10, 0))
   local char = plr.Character or plr.CharacterAdded:Wait()
  local _, carSize = newCar:GetBoundingBox() 
char:PivotTo(newCar:GetPivot()*CFrame.new(carSize.X / 2 + 5, 0, 0))
		
		newCar.Parent = workspace
		
		plrCars[plr] = newCar
	end
end)
3 Likes

Thank you so much for your help!

2 Likes