ents.FindInBox() Deletion

I want to delete all the entities found within the spawn of the map.
[lua]local SpawnEnts = ents.FindInBox( Vector(784.556458, -497.887848, 95.031250), Vector(1271.556274, 498.265106, 350.14038) )[/lua]

How would I call up all the entities found in the box and delete them? I’m now sure how they are saved (Table, etc.) so I have no idea how to approach this.

[editline]20th July 2011[/editline]

I found the solution, I think.

[lua]
function Blockspawn()
for _, v in pairs (ents.FindInBox( Vector(784.556458, -497.887848, 95.031250), Vector(1271.556274, 498.265106, 350.14038) )) do
if !v:IsPlayer then
v:Remove()
end
end
end
[/lua]

[lua]
for _, v in pairs(ents.FindInBox( Vector(784.556458, -497.887848, 95.031250), Vector(1271.556274, 498.265106, 350.14038) )) do
if IsValid(v) && !v:IsPlayer() then v:Remove() end
end
[/lua]

[editline]20th July 2011[/editline]
Didn’t see your edit, yep that should work but i’d suggest testing for validity too.


[gamemodes\sandboxsd\gamemode\init.lua:44] function arguments expected near 'then'


[lua]
function Deletespawnprops()
for _, v in pairs (ents.FindInBox( Vector(784.556458, -497.887848, 95.031250), Vector(1271.556274, 498.265106, 350.14038) )) do
if !v:IsPlayer then --This is line 44
v:Remove()
end
end
end
timer.Create( “SpawnBlockProtection”, 5, 0, Deletespawnprops )
[/lua]

[editline]20th July 2011[/editline]

Derp, there were no function “()”

Is it really a good idea to remove all entities other than player?

Wouldn’t it be better to only search for prop_physics ents (and the likes)?

I use something like this in my utility blocks addon:
[lua]
local noPropEnts = { “prop_physics”, “prop_dynamic”, “prop_vehicle” }
for _, ent in pairs(ents.FindInBox(v.origin-vec,v.origin+vec)) do
for k,v in pairs(noPropEnts) do
if string.find(v, ent:GetClass()) then
if ValidEntity(ent) and !ent:IsPlayer() then
local dissolver = ents.Create(“env_entity_dissolver”)
dissolver:SetKeyValue(“dissolvetype”,3)
dissolver:SetKeyValue(“magnitude”,1)
dissolver:SetPos(ent:GetPos())
dissolver:SetKeyValue(“target”,ent:GetClass())
dissolver:Spawn()
dissolver:Fire(“Dissolve”,ent,0)
dissolver:Fire(“kill”,"",0.1)
end
end
end
end
[/lua]

-snip-

[editline]20th July 2011[/editline]

Also, It deletes all the props on the server if there is one prop on spawn. Anyone know why? I’ve been trying to figure it out for like an hour.

If it helps.

[lua]

function Deletespawnprops()
local noPropEnts = { “prop_physics”, “prop_dynamic”, “prop_vehicle” }
for _, ent in pairs(ents.FindInBox(Vector(769.6429, -511.4517, 31.0313),Vector(1275.5303, 509.6656, 292.8974))) do
for k,v in pairs(noPropEnts) do
if string.find(v, ent:GetClass()) then
if ValidEntity(ent) and !ent:IsPlayer() then
local dissolver = ents.Create(“env_entity_dissolver”)
dissolver:SetKeyValue(“dissolvetype”,3)
dissolver:SetKeyValue(“magnitude”,1)
dissolver:SetPos(ent:GetPos())
dissolver:SetKeyValue(“target”,ent:GetClass())
dissolver:Spawn()
dissolver:Fire(“Dissolve”,ent,0)
dissolver:Fire(“kill”,"",0.1)
end
end
end
end
end
timer.Create( “SpawnBlockProtection”, 5, 0, Deletespawnprops )
[/lua]

[editline]20th July 2011[/editline]

bump

[lua]
dissolver:SetKeyValue(“target”,ent:GetClass())
[/lua]
Most props have the same class (which is prop_physics) (there are a few others like prop_ragdoll)

So would just making it target “ent” work?

Bump, ^^^ Didn’t work.

If you read that, you’ll see you can’t target a specific entity with the env_entity_dissolver unless it has a unique name. (and entities don’t have names by default).

This is true, what you could do is use **[Entity:SetKeyValue()

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetKeyValue)** to set the targetnames of the ents to be removed, and then use that name with the env_entity_dissolver.

Made this awhile ago…

[lua]
function SpawnProtection()

local spawnpos = {}
spawnpos["rp_downtown_v2"]=Vector(-2675.880615,-2425.776123,-139.968750)

for l,m in pairs(spawnpos) do
	if l == game.GetMap() then
		for k,v in pairs(ents.FindInSphere(m,600)) do
			if v:GetClass() == "prop_physics" then
				v:Remove()
				if v.Owner then
					Notify(v.Owner,1,4,"Prop removed due to being near spawn.")
				end
			end
		end
	end
end

end
hook.Add(“Think”,“SpawnProtection”,SpawnProtection)
[/lua]

That won’t work since it’s only doing it once and not all the time.

It’s an example, obviously.

[lua]hook.Add( “PlayerSpawnedProp”, “c00l_effects”, function( ply, mdl, ent )
ent.Owner = ply
ent:SetKeyValue( “targetname”, “prop_physics_” … ply:UniqueID() )
end )

hook.Add( “PlayerSpawnedVehicle”, “c00l_effects”, function( ply, ent )
ent.Owner = ply
ent:SetKeyValue( “targetname”, “prop_vehicle_” … ply:UniqueID() )
end )

function Deletespawnprops()
local noPropEnts = { “prop_physics”, “prop_vehicle” }
for _, ent in pairs( ents.FindInBox( Vector( 769.6429, -511.4517, 31.0313 ), Vector( 1275.5303, 509.6656, 292.8974 ) ) ) do
for , v in pairs( noPropEnts ) do
if string.find( v, ent:GetClass() ) then
if ValidEntity( ent ) then
local dissolver = ents.Create( “env_entity_dissolver” )
dissolver:SetKeyValue( “dissolvetype”,3 )
dissolver:SetKeyValue( “magnitude”,1 )
dissolver:SetKeyValue( “target”, "Format( "%s
%s", ent:GetClass(), ent.Owner:UniqueID() ) )
dissolver:Spawn()
dissolver:Fire( “Dissolve”, ent, 0 )
dissolver:Fire( “Kill”, “”, 0.1 )
end
end
end
end
end
timer.Create( “SpawnBlockProtection”, 5, 0, Deletespawnprops )[/lua]

Enjoy.

-herp-

Actully it was supposed to be on Think hook, dont know why it on InitPostEnt, edited.

Another way of doing it (just for reference) would be to create a PlayerSpawn hook, and then use the ents.FindInBox() function to remove all of the props, vehicles, NPCs etc. around the player’s spawn location. It does mean that entities will only be removed if a player uses that spawn point, and they won’t know until a player spawns, but it saves having to add a vector for each map.

Ahem. PlayerSpawn ? You mean everytime the player respawns? I somewhat don’t think so. I will look up the proper hook.

[editline]23rd July 2011[/editline]

**[Gamemode.OnEntityCreated

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.OnEntityCreated)**

You will need to perform all the checks, example on wiki

Can I ask what exactly is wrong with using that hook to detect the location of the the spawn point? I presume that Hyper Iguana wants to ensure that players don’t spawn into props, vehicles etc. and the best way to detect where a spawn location is to use the PlayerSpawn hook and to immediately retrieve the player’s location.