NPC's hold weapon after death

I don’t know if this has been made yet but It would be cool if there was a script to have Npc’s hold on to their weapons after death.

Oddly enough, I don’t think it’s been done before. I want this too

It would be col wouldnt it

For this to work you’d have to generate all ragdolls for dead NPCs server-side. (which can be done)

Then You’d have to get their weapon they were using. (also… this is too easy)

Then you’d have to parent the weapon to their hand bone.

All-in-all, I think this could be done relatively easy, however depending on if you’d be using this on a MP server and how many NPCs there were dying at a time, and how long they stayed before despawning… this could cause a lot of server side lag.

since all dead npcs have to be serverside, it would cause lag.

especially when someone gets too excited with explosives.

Eh, do it anyways, I’d probably only use it in singleplayer anyways.

You can’t pick them up afterwards since it’s only an effect, but put this in autorun/npc_ragdoll_weapons.lua

[lua]if SERVER then
AddCSLuaFile( “npc_ragdoll_weapons.lua” )

umsg.PoolString( "Store Weapon Model" )

local function OnNPCKilled( npc )
	if not ValidEntity( npc:GetActiveWeapon( ) ) then
		return
	end
	
	umsg.Start( "Store Weapon Model" )
		umsg.Entity( npc )
		umsg.String( npc:GetActiveWeapon( ):GetModel( ) )
	umsg.End( )
	
	npc:GetActiveWeapon( ):Remove( )
end

hook.Add( "OnNPCKilled", "KeepWeapon.OnNPCKilled", OnNPCKilled )

else
local stored = { }
local keep = GetConVar( “ai_keepragdolls” )

local function umOnNPCKilled( um )
	local t
	
	t = { }
	t.Npc = um:ReadEntity( ):GetModel( )
	t.Weapon = ClientsideModel( um:ReadString( ), RENDERGROUP_OPAQUE )
	t.Weapon:SetNoDraw( true )
	table.insert( stored, t )
end

local function OnEntityCreated( ent )
	local k, v, chose
	
	if not ValidEntity( ent ) then
		return
	end
	
	if not keep:GetBool( ) then
		if ent:GetClass( ) == "class C_ClientRagdoll" then
			for k, v in pairs( stored ) do
				if v.Npc == ent:GetModel( ) then
					chose = k
					
					ent.M = v.Weapon
					ent.M:SetNoDraw( false )
					ent.M:AddEffects( EF_BONEMERGE )
					ent.M:SetParent( ent )
					break
				end
			end
		end
	else
		if ent:GetClass( ) == "prop_ragdoll" then
			for k, v in pairs( stored ) do
				if v.Npc == ent:GetModel( ) then
					chose = k
					
					ent.M = v.Weapon
					ent.M:SetNoDraw( false )
					ent.M:AddEffects( EF_BONEMERGE )
					ent.M:SetParent( ent )
					break
				end
			end
		end
	end
	
	if chose then
		stored[ chose ] = nil
	end
end

usermessage.Hook( "Store Weapon Model", umOnNPCKilled )
hook.Add( "OnEntityCreated", "KeepWeapon.OnEntityCreated", OnEntityCreated )

end[/lua]

Was pretty neat, but I feel there’s probably a better solution that supports both types of ragdolls.

I don’t see why there’s any reason not to trust the client’s to not lie about picking up the weapon. You could do the check on the client and simply tell the server to remove the gun for any other player who wants to pick it up. The only way it could paradox is if there’s a fuckload of moving props that get in the way of it, the player drops a bunch of packets, or he intentionally hacks something.

Well the script works pretty darn good it just leaves behind a gun model after the ragdoll i cleaned up.