Something that counts how many props are on the server

Could someone make a thing where if you type a certain ConVar it tells you the amount props spawned (not world props) on the map? That would be great.

UNTESTED !!! POST IF IT BRAKES

[LUA]

local old_ent_create = ents.Create

local Spawned = { }

function ents.Create( name )
if name == ( ‘prop_dynamic’ or ‘prop_dynamic_ornament’ or ‘prop_dynamic_override’ or ‘prop_physics’ or ‘prop_physics_multiplayer’ or ‘prop_physics_override’ ) then
local ent = old_ent_create( name )
Spawned[ #Spawned + 1 ] = ent
return ent
else
return old_ent_create( name )
end
end

function ents.GetAllSpawnedEnts()
return Spawned
end

[/LUA]

use ents.GetAllSpawnedEnts() to get all player spawned ents. it will return a table with the entitys. :science:

hope i helped

That’s not a good way of doing it. You could’ve just used ents.FindByClass instead, and that also gets entities not spawned by players.
[lua]local entities = {};

hook.Add(“PlayerSpawnedProp”, “ents.GetAllSpawned”, function(ply, _, ent)
table.insert(entities, ent);

ent:CallOnRemove("ents.GetAllSpawned", table.remove, entities, ent);

end);

concommand.Add(“propcount”, function(ply)
ply:PrintMessage(HUD_PRINTCONSOLE, #entities);
end);[/lua]

Didn’t know that hook existed, sorry

[editline]12:15PM[/editline]

thats why i hooked into the ents.Create function

Thanks! :cheers:
Edit:
Does it go under autorun/server?

Yes

Oh, well, I tested it and when ever I did the cmd it said 0 props, can you fix it?


local entities = {};

hook.Add("PlayerSpawnedProp", "ents.GetAll", function(ply, _, ent)
    table.insert(entities, ent);
    
    ent:CallOnRemove("ents.GetAllSpawned", table.remove, entities, ent);
end);

concommand.Add("propcount", function(ply)
    ply:PrintMessage(HUD_PRINTCONSOLE, #entities);
end);

[editline]12:03PM[/editline]

**[Ents.GetAll

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Ents.GetAll)**, not Ents.GetAllSpawned