Remove npc weapons laying around

I think it’s a rather simple thing, but I can’t figure it out on my own.
I am searching for a few lines of lua, that delete all npc weapon entities (all ar2, combineballs, etc.), to prevent Npcs from dropping ammunition and guns.
This is a feature I always wanted in garrysmod.
Can anyone tell me how to delete npc weapons on the ground via lua, or is there an even simpler solution?
thx in advance

edit: perhaps there might even be a console command to clean up all weapons, that I could trigger via lua. Anyone knows, if such a console command exists?

Add this hook Serverside:

[lua]
hook.Add(“OnNPCKilled”, “RemoveWeaponsFromNPC”, function(KilledNPC)
for _, W in pairs(ents.GetAll()) do
if W:IsWeapon() && W:GetOwner() == KilledNPC then
W:Remove()
end
end
end)
[/lua]

Unfortunately, as soon as the NPC dies, it’s weapons lose their ‘owner’ and cannot be identified as belonging to the NPC. The only way to remove weapons lying around is to remove ALL of them, like this:

[lua]
concommand.Add(“ClearWeaponsLyingAround”, function(ply)
for _, W in pairs(ents.GetAll()) do
if W:IsWeapon() && (W:GetOwner() == NULL || !W:GetOwner():IsValid()) then
W:Remove()
end
end
end)
[/lua]

wow thanks, I really apprechiate that.
Works great. weapons vanish, before they hit the ground.
It doesnt really bother me, but do you know why CombineBalls do not get removed?
sincerly

‘Combine Balls’ are not weapons, they are ammunition. Try this instead:
[lua]
hook.Add(“OnNPCKilled”, “RemoveWeaponsFromNPC”, function(KilledNPC)
for , W in pairs(ents.GetAll()) do
if (W:IsWeapon() || string.find(W:GetClass(), "item_ammo
")) && W:GetOwner() == KilledNPC then
W:Remove()
end
end
end)
[/lua]

awesome work, man.
I hope I don’t go too far with my request, but could you add a console command to toggle it on/off.
I would love you for this.
sincerly

Use ‘npc_allow_weapon_drop’ with 0 or 1 to enable/disable NPC’s dropping weapons.

[lua]
CreateConVar(“npc_allow_weapon_drop”, 1, { FCVAR_NOTIFY, FCVAR_ARCHIVE, FCVAR_REPLICATED })

hook.Add(“OnNPCKilled”, “RemoveWeaponsFromNPC”, function(KilledNPC)
if tobool(GetConVarNumber(“npc_allow_weapon_drop”)) then return end
for , W in pairs(ents.GetAll()) do
if (W:IsWeapon() || string.find(W:GetClass(), "item_ammo
")) && W:GetOwner() == KilledNPC then
W:Remove()
end
end
end)
[/lua]

epic

-snip-