Scripting entities

I’ve asked around and nobody seems to understand how to help me.

I have an entity that is supposed to give you health when you use it, if you are below 200 hp. My problem is that when I am over 200 hp and i use the entity, it just deleted the physics and model.


function ENT:Use( ply )

	if ( ply:IsPlayer() ) then

		if( ply:Health() < 200 ) then

			sound.Play( "vo/npc/male01/moan0".. math.random(1,5) ..".wav", self:GetPos( ) )
			ply:SetHealth( ply:Health() + math.random(10,25))
			self:Remove()
      	        end
		
	end
	
end

http://cloud-4.steampowered.com/ugc/594785681589273970/586FBFB7390B2EFE755BAA581E0E09CCF76A1446/

Help is appreciated, thanks.

[editline]23rd May 2014[/editline]

Posted the wrong code, I updated it. Sorry!

So, basically, your code has a syntax error in it.

Here’s fixed one:



function ENT:Use( ply )

	if ( !ply:IsPlayer() ) then return end -- This looks cleaner, don't you agree?
	
	if ( ply:Health() >= 200 ) then self:EmitSound( "vo/Citadel/br_no.wav" ) return end -- If the player has 200 or more HP, play a sound and bail the function using return

	-- Nothing above is true. Player must be lower than 200 HP. Do some magic
	self:EmitSound( "vo/npc/male01/moan0" .. math.random( 1, 5 ) .. ".wav" ) -- self:EmitSound works best for entities, no need for sound.Play
	ply:SetHealth( ply:Health() + 15 )
	self:Remove() -- Remove entity

end

[editline]23rd May 2014[/editline]

I am sure you will be able to adapt this code for your needs.

Even with the exact code you posted I’m still getting the same problem. :suicide: I don’t understand lua.

Maybe I’ll just skip it and it will click when i have a fresh look at it again tomorrow. Any other ideas are welcome.

Something else in your code removes it. Does the “no” sound even plays?

never mind.

Sorry for the long response, in that example of code the ‘no’ sound plays but still removes the model and physics.

I also have this little set up which does the same thing.


function ENT:Use( ply )

	if ( ply:IsPlayer() ) then

		if( ply:Health() < 200 ) then

			sound.Play( "vo/npc/male01/moan0".. math.random(1,5) ..".wav", self:GetPos( ) )
			ply:SetHealth( ply:Health() + math.random(10,25))
			self:Remove()
      	        end
		
	end
	
end

If you remove the “self:Remove()” bit, does it still remove the entity? Does the sound play? Do you get HP?

When I removed the “self:Remove()” line I was able to hold E and shoot up in health as it played sounds. It still deleted the model and physics though. very frustrating.

Well, it’s been all day and still nothing seems to work. I am just going to make it to where if you are above 200 hp you will gain 1 hp. Thanks very much for even reading everybody!