Car spawning Script doesn't work as i wanted

Well so i saw this script and it seems to work just fine, but i see that the cars overlap with each other and bug out when you spawn it on the same place, is there a way to make them not overlap and a limit like (5 cars only) to be less laggy and unbugged? i have been looking for something similar, no luck so far.

In others words, a car spawn script that has a limit and cannot overlap with each other.

script.Parent.MouseButton1Click:connect(function(GetCar)
		Mod = game.ServerStorage.Car1     --Change test to whatever you have named the item you want to spawn
		clone = Mod:clone()
		clone.Parent = workspace
		clone:MakeJoints()
end)

I would create a for loop that adds one to a list every time a car is spawned. If the value is <= 5, don’t spawn a car. If the value is under 5, then allow it to.

Perhaps adding a slight wait (like 5-10) would eliminate the issue of cars spawning on top of each other.

If that doesn’t work, add a .Touched function to check if a model with the name ‘Car1’ is touching it, and if it is, don’t spawn a car.

Hope this helps :slight_smile:.

2 Likes

what functions or stuff can do that? like how to check there is 5 of the same model?

1 Like

You could use a for loop for counting up, and an if statement for checking if there is five of the same model (if i == 5 then).

using a for loop with workspace:GetDescendants would work for limiting the amount of cars. (iterate through every descendant of workspace and check if it is a car). As for making sure they don’t touch, you could put a part over where they spawn, and use part:GetTouchingParts. This should return an array of things touching it. If you check for a car within this array, you just don’t spawn another car. I hope this helps, good luck!

1 Like