Need help creating death NPC ragdoll

So basicly what I’m asking for is that how to properly create an NPC death ragdoll. So far I got something like this this :

[lua]
function ENT:OnTakeDamage(dmg)

self:Bleed(dmg)

self:SetHealth(self:Health() - dmg:GetDamage())

if (self:Health() <= 0 && self.Dead == false) then 
	self.Dead = true
	self:Killed(dmg)
end

end

function ENT:Bleed(dmg)

local bleed = ents.Create("info_particle_system")
bleed:SetKeyValue("effect_name", "blood_impact_red_01")
    bleed:SetPos(dmg:GetDamagePosition()) 
bleed:Spawn()
bleed:Activate() 
bleed:Fire("Start", "", 0)
bleed:Fire("Kill", "", 0.2)   

end

function ENT:Killed(dmg)

self:EmitSound(self.Death,350,math.random(80,100))
local ragdoll = ents.Create("prop_ragdoll")
ragdoll:SetModel( self:GetModel() )
ragdoll:SetPos( self:GetPos() )
ragdoll:SetAngles( self:GetAngles() )
ragdoll:Spawn()
ragdoll:SetCollisionGroup( COLLISION_GROUP_DEBRIS ) 
ragdoll:SetSkin( self:GetSkin() )
ragdoll:SetColor( self:GetColor() )
ragdoll:SetMaterial( self:GetMaterial() )

undo.ReplaceEntity(self,ragdoll)
cleanup.ReplaceEntity(self,ragdoll)

self:Remove()

end
[/lua]

Now this thing works as intended, however the ragdoll is an other entity created that just seems to not set bones positions relatively to the SNPC when it dies. What I want is to set bones for the ragdoll to have same vectors and angles as the SNPC when it dies. Without it the ragdoll will just appear in T-pose and fall to ground. What function do I need to use?

You can use prop_ragdoll, but ClientSide ragdolls are much better, they also (to my knowledge) automatically assign bone positions to the positions on the SNPC.

If you go in to your cl_init.lua and add the following code:


net.Receive("sv-cl-rgdll-start",function()
local mEnt = net.ReadEntity()
	if IsValid(mEnt) then
		local ent = mEnt:BecomeRagdollOnClient()
	end
end) 

In init.lua, you could have:


function ENT:Killed(dmg)	

	self:EmitSound(self.Death,350,math.random(80,100))

	util.AddNetworkString("sv-cl-rgdll-start")
	net.Start("sv-cl-rgdll-start")
	net.WriteEntity(self)
	net.Broadcast("sv-cl-rgdll-start")

	self:Remove()
	
end 

To the similar effect, you could do this:



self:SetNPCState(NPC_STATE_DEAD)
self:SetSchedule(SCHED_DIE_RAGDOLL)


In the case that you want to still use prop_ragdoll, I’ve got nothing… these examples are really more advanced.

Nice, the one that uses Networking library works well, but I’m wondering, how can I add velocity to the death ragdoll?

[lua]ent:SetVelocity(mEnt:GetVelocity())[/lua]
Should do the trick.

Alright got it working, just needed to write net.WriteVector() in the network sending part to return the Vector of dmg:GetDamageForce() to properly set the velocity of the ragdoll. The code now looks like this :

[lua]

function ENT:Initialize()
if SERVER then
util.AddNetworkString(“client_ragdoll”)
end
end

function ENT:Bleed(dmg)

local bleed = ents.Create("info_particle_system")
bleed:SetKeyValue("effect_name", "blood_impact_red_01")
    bleed:SetPos(dmg:GetDamagePosition()) 
bleed:Spawn()
bleed:Activate() 
bleed:Fire("Start", "", 0)
bleed:Fire("Kill", "", 0.2)

end

function ENT:OnTakeDamage(dmg)

self:Bleed(dmg)

self:SetHealth(self:Health() - dmg:GetDamage())

if (self:Health() <= 0 && self.Dead == false) then 
	self.Dead = true
	self:Killed(dmg)
end

end

function ENT:Killed(dmg)

self:EmitSound("npc/zombie/zombie_die1.wav",350,math.random(80,100))
local vec = dmg:GetDamageForce()

net.Start("client_ragdoll")
	net.WriteEntity(self)
	net.WriteVector(vec)
net.Broadcast("client_ragdoll")

undo.ReplaceEntity(self,ragdoll)
cleanup.ReplaceEntity(self,ragdoll)

self:Remove()

end

if CLIENT then
net.Receive(“client_ragdoll”,function()
local ent = net.ReadEntity()
local vector = net.ReadVector()
if ent:IsValid() then
local ragdoll = ent:BecomeRagdollOnClient()
ragdoll:GetPhysicsObject():SetVelocity( ent:GetVelocity() + vector )
end
end)
end
[/lua]


 undo.ReplaceEntity(self,ragdoll)
	cleanup.ReplaceEntity(self,ragdoll)


Hm, these are server-only functions, you cannot use this within a Clientside piece of code, unfortunately.

Alright, good to know.