Spawning Entities [TTT]

I’m making a gift entity for TTT, because of Christmas. I’ve pretty much got the entity down from working with the hp station code, but how do I drop it on a player’s death?
I know I’d be hooking a function to PlayerDeath, but how would I spawn the hp station? Would this work:

[lua]

function SpawnGift( v, i, k )

 local ch = math.random(1, 4)
 pos = v:GetPos()

 if ch == 1 then return end

 else

 local gift = ents.Create( "ttt_gift" )
 if IsValid( gift ) then
      gift:SetPos ( pos )
      gift:Spawn()  
 end

end

hook.Add( “PlayerDeath”, “SpawnGift”, SpawnGift )

[/lua]

Why not try it instead of asking if it would work lol?

syntax error, try it and you would know.

the ifs are a little funny

Why?

I’ve had no chance to try this, since my computer is currently broken and I’m using one which has no games on it.


     if ch == 1 then return end

What about it?

He’s being stupid. Remove the end from after your return statement.

Edit: Even better:




local PercentChanceForGift = 25 -- change this to whatever you'd like, as long as it's between 0 and 100.

local function SpawnGift( victim )
     if math.Rand( 0, 100 ) <= PercentChanceForGift then
	 local ent = ents.Create( "ttt_gift" )
         ent:SetPos( victim:GetPos() )
         ent:Spawn()  
     end
end

hook.Add( "PlayerDeath", "SpawnGift", SpawnGift )



You mean math.random not math.Rand, and you can just do “math.random() <= 0.25”

I’m pretty sure math.Rand works as well

both math.Rand and math.random work equally in this instance. Rand is easier to type than random, so I use that. Whether I use a float or whole number is irrelevant also as long as everything is scaled by the appropriate power of 10. I use a whole numer because it’s more intuitive to someone who may have little experience with coding, as indicated by the code that OP pasted.