Removing Map Props Through Lua

So I would like to remove a map prop through Lua there are a good 8 of these props I need to get rid of. How would I go about making a script so that everytime the server starts it removes those props from the map. And I have looked at posts from the past but none of them have seen to work and some recommend hammer. The reason I won’t use hammer is the map is huge and my PC most likely wouldn’t be able to handle its size. Thanks in advance though I would love to get this done.

ents.GetMapCreatedEntity

How would you go about using that in a script?

:snip: Jason’s solution is better

That’s not necessary if you’re using

ents.GetMapCreatedEntity

On PostEntityInitialize, loop through all prop_physics entities, and then if it has the desired model, remove it. simple.

PostEntityInitialize doesn’t exist :stuck_out_tongue:

Use InitPostEntity instead.
Just to give you a tiny example:



local toRemove = {
      [ 'models/props_building_details/Storefront_Template001a_Bars.mdl' ] = true,
      [ 'models/props_borealis/bluebarrel001.mdl' = true,
      [ 'addasmany' ] = true
}

hook.Add( 'InitPostEntity', 'removeProps', function()

      local props = ents.FindByClass( 'prop_physics' ) -- cache them in here
      
      for i = 1, #props do -- faster method
            local ent = props[ i ] -- Get the entity from the table we created above with the id 'i'

            if toRemove[ ent:GetModel() ] then -- Check if the model exists in the toRemove table. Faster than table.HasValue()
                  ent:Remove() -- Remove the prop
            end
  
     end
      
end )


Thats what I mean, was too lazy to open the wiki :3

Ya, it doesn’t seem to be working my code is bellow. Just so you know I placed this code in lua/autorun/server/map_removal.lua


local toRemove = {
      [ 'models/statua/shell/shellpump1.mdl' ] = true
}

hook.Add( 'InitPostEntity', 'removeProps', function()

      local props = ents.FindByClass( 'prop_physics' ) -- cache them in here
      
      for i = 1, #props do -- faster method
            local ent = props[ i ] -- Get the entity from the table we created above with the id 'i'

            if toRemove[ ent:GetModel() ] then -- Check if the model exists in the toRemove table. Faster than table.HasValue()
                  ent:Remove() -- Remove the prop
            end
  
     end
      
end )

Not really sure why I’m spoonfeeding but here’s some code:

[lua]local maps = {
gm_construct = {
[2415] = true
}
}

hook.Add(“InitPostEntity”, “DamnDoll”, function()
local moveents = maps[game.GetMap()]
if not moveents then return end
for k, v in pairs(moveents) do
local ent = ents.GetMapCreatedEntity(k)
if IsValid(ent) then
ent:SetPos(Vector(0, 0, 0))
end
end
end)[/lua]

This will put the doll from the hidden room in gm_construct at the map origin on spawn. Adapt to whatever.

Should I put this in Useful Code Snippets?

[editline]22nd October 2016[/editline]

No idea why I made the table structure like that

How did you find the number id of the doll? And if you were to set it to false would it not appear there?

Better code:
[lua]local maps = {
gm_construct = {
2415, 2416, 2417, 2426
}
}

hook.Add(“InitPostEntity”, “DamnDoll”, function()
local moveents = maps[game.GetMap()]
if not moveents then return end
for _, MapCreationID in ipairs(moveents) do
local ent = ents.GetMapCreatedEntity(MapCreationID)
if IsValid(ent) then
ent:SetPos(Vector(0, 0, 0))
end
end
end)[/lua]

[editline]22nd October 2016[/editline]

To find the MapCreationID of the entity, open the map in singleplayer, look directly at the entity and run this command in the console:

lua_run print(Entity(1):GetEyeTrace().Entity:MapCreationID())



hook.Add('InitPostEntity' 'removeprops', function()
	for k, v in pairs(ents.FindByClass('prop_physics')) do
		if v:GetModel() == 'YOURMODEL' then
			v:Remove()
		end
	end
end)


Sigh.

[editline]22nd October 2016[/editline]

I doubt he wants to look at every prop he wants to remove and do that… simply loop through them like shown above from me…

Thankyou for your help it worked perfectly.

Thanks for all your help though especially NeatNit, whitestar, and Tupac. :slight_smile:

:snip:

You’re welcome!

Uhh no, that’s probably exactly what he wants to do.
It’s also the best way of removing these props, because most of the times you want to remove a button, not ALL buttons. Or 1 prop blocking something, not all of said props in the map

This will remove all prop_physics 1 second after server start.


if SERVER then
        timer.Create("cleanprops", 1, 1, function()
	        for k, v in pairs(ents.FindByClass("prop_physics")) do v:Remove() end
        end)
end

Oh… Can I rate myself dumb?

There’s no need to create a timer like that if we have GM:InitPostEntity. That method would be unreliable and it’s just shitty practice in general.

Don’t forget about function ents.GetMapCreatedEntity check all entities for MapCreationID. Make one cycle is better than count of table cycles.

It doesn’t matter in the slightest. This code runs once.