Rather than deleting props if they collide, I would rather make sure the space isn’t occupied. Try something like this.
[lua]function SpawnProp(model,times,radius,overflow) – You don’t use overflow. You just call SpawnProp(model,times,radius).
if not overflow then overflow = 0 end
if overflow>5 then return end -- We've tried 5 times; we just can't find an open spot! Give up. :(
local fails = 0
for i=1,times do
local p = Vector(math.random(4000),math.random(4000),19)
if #ents.FindInSphere(p,radius)<1 then
local e = ents.Create("prop_physics")
fails = fails + 1 -- We keep track of all the times we picked an occupied spot, so we can try again when we're done with this spawn iteration.
if fails>0 then
SpawnProp(model,fails,radius,overflow+1) -- We failed at least once, so we'll try to spawn those ones that failed again.
Where “radius” is the area around the prop you don’t want anything else to be. I suggest something slightly larger than the prop itself.
It’s a bit more complex than the code you’re trying to achieve, but that’s because I put in an overflow prevention. Look over the code, see if it all makes sense. Any questions, don’t hesitate to ask. I put comments on the overflow logic to help you see what I’m doing.
Also, just a quick note. When you do math.random(int), the generated number will only be between 0 and 4000. This is fine and all, but assuming the origin of the map (Vector(0,0,0)) is in the center of your map, you’ll only be spawning props in the upper-right region of the map doing this.
I suggest doing math.random(-int,int), which in this case is math.random(-4000,4000), for both x and y. That way you get all four quadrants of space.