Remove weapons entities from server

Hello, I’m trying to remove all weapons entities from server, but no sucess =/

This is my code:

function RemoveWeaponsAndItems()
	for k, wep in pairs(ents.FindByClass("weapon_*")) do
	for k, item in pairs(ents.FindByClass("item_*")) do
hook.Add("InitPostEntity", "PH_RemoveWeaponsAndItems", RemoveWeaponsAndItems)

for k,ent in pairs(ents.GetAll()) do
if ent:IsWeapon() then

Pretty sure that hook runs when the map is loaded. Try to have it not in a hook like that, but in a console command or something similar.

InitPostEntity is the correct hook to use when adding or removing entities on startup. Basically that hook is called when all of the map entities have already spawned, so by spawning more you won’t shift their EntIndex in that hook.

I’d recommend doing what Xaotic is recommending, that way you’re only looping through all entities once instead of twice. ents.Find* functions can be costly.

hook.Add( "InitPostEntity", "PH_RemoveWeaponsAndItems", function( )
	for k, _ent in pairs( ents.GetAll( ) ) do
		// Worldspawn is NULL, skip in that case..
		if ( !IsValid( _ent ) ) then continue; end

		// If the item classname starts with item_ then this var will be true
		local _bItem = string.StartWith( _ent:GetClass( ), "item_" );

		// If the entity is a weapon, this var will be true ( The vars don't need to be here, just showing alternative methods )
		local _bWeapon = _ent:IsWeapon( );

		// Now, if either var is true, remove the entity safely ( checks IsValid, etc... Things can change during a loop and errors can occur )
		if ( _bWeapon || _bItem ) then
			SafeRemoveEntity( _ent );
end );

I think hes trying to remove all weapons later in the game, and not in startup.

I’m trying to remove all the weapons on map start, because my server is a Prop Hunt server and some people are using RPG, making the game unfun

My solution will work for that. InitPostEntity is called on the Server when the map has finished loading its entities ( weapons included ). Now, if the game-mode itself is adding the weapons you’ll need to find that script and modify it.