How to remove prop_ragdoll when I Spawn ?

There is all in the title.

Thanks in advance.

[lua]
if SERVER then

function PlyRagodlize( ply, ent )

local ent = ents.Create( “prop_ragdoll” )

	if ent:IsValid() then
		ent:SetModel( ply:GetModel() )
		ent:SetKeyValue( "origin", ply:GetPos().x .. " " .. ply:GetPos().y .. " " .. ply:GetPos().z )
		ent:Spawn()
		ent:Activate() 
		ent:SetVar("ragdollname",ply:Nick().."")
		
	ply:DrawViewModel(false)
	ply:Spectate( OBS_MODE_CHASE )
	ply:SpectateEntity( ent )
	ply:StripWeapons()

if ply:GetWeapons() == "weapon_physgun" then
	ent:Remove()

end

end

end

concommand.Add(“Ragdollize”, PlyRagodlize)

function Spawnc( ply )

local P1 = player.GetByID(1)
local pos = P1:GetPos()
P1:Spawn()

end
concommand.Add(“Spawnss”, Spawnc )

end[/lua]

Oh wow, would you please indent your code properly and fix those typing mistakes, that really doesn’t make anyone want to help you.

[lua]if SERVER then

function PlyRagdollize( ply, ent )

local ent = ents.Create( "prop_ragdoll" )

if ent:IsValid() then
	ent:SetModel( ply:GetModel() )
	ent:SetKeyValue( "origin", ply:GetPos().x .. " " .. ply:GetPos().y .. " " .. ply:GetPos().z )
	ent:Spawn()
	ent:Activate() 
	ent:SetVar("ragdollname",ply:Nick().."")
	
	ply.RagdollEnt = ent
	ply:DrawViewModel(false)
	ply:Spectate( OBS_MODE_CHASE )
	ply:SpectateEntity( ent )
	ply:StripWeapons()
	
	if ply:GetWeapons() == "weapon_physgun" then
		ent:Remove()
	end
end

end

concommand.Add(“Ragdollize”, PlyRagdollize)

function Spawnc( ply )

local P1 = player.GetByID(1)
local pos = P1:GetPos()
P1:Spawn()

if ValidEntity(ply.RagdollEnt) then
	ply.RagdollEnt:Remove()
end

end

concommand.Add(“Spawnss”, Spawnc )

end[/lua]

You can store the ragdoll entity in a variable, like that, this way when a player spawns, all you need to do is grab that variable, see if it contains a valid ragdoll, and if it does, remove it.

Also, I don’t see the point to “spawnss”, are you new to Lua or what?

Thanks it’s working :slight_smile:

Yeah I know it’s not original but I just trying some code for learning but if I put “Spawn” only I taking a chance this concommand was taking by another addons and make an error ?

I’m new (since 1 year, but I take my time) but I try sometime and read sometime the wiki.