Glitch or programming error with SetHealth entity?

This doesn’t remove itself unless I disable ‘if ply:Health() >= 100 then return end’, but why?

It seems like the code reloops itself.


function ENT:Use( ply )

	--if ply:Health() >= 100 then return end

	        ply:SetHealth( ply:Health() + 75 )
		if ply:Health() >= 100 then 
		ply:SetHealth(100) 
		end
	self:Remove()

end

Because return keyvalue prevents any further code execution.

I rewrote it like this and still the same effect.


function ENT:Use( ply )

	if ply:Health() < 100 then

		ply:SetHealth( ply:Health() + 75 )
		if ply:Health() >= 100 then 
		ply:SetHealth(100) 
		end
	self:Remove()
	end
end

It will not remove itself if players health is less or equal to 100. That’s what it is coded to do.

Also, you could do something like this:



function ENT:Use( ply )

    if ply:Health() < 100 then
        ply:SetHealth( math.min( ply:Health() + 75, 100 ) )
        self:Remove()
    end
end

I don’t understand what’s happening.

I thought that after adding the health it should remove itself regardless of the conditions in the middle.

You want to tell me that it is not removed after the health is added? I find that hard to believe. Unless you have overridden the ENT:Remove somewhere?

I don’t believe it either, not really sure why it’s happening.

My other functions can remove the entity just fine.

[editline]14th May 2014[/editline]

Do print( ply:Health() ) and post what it says in console. You should’ve done that before.

I tried that earlier, this is what I get.

Well, as I said bunch of posts earlier…

Please, PAY ATTENTION.



function ENT:Use( ply )

    if ply:Health() < 100 then
        ply:SetHealth( math.min( ply:Health() + 75, 100 ) )
        self:Remove()
    end
end

Is this suppose to remove the entity? Because it doesn’t.

My god, you are really dense.

[lua]function ENT:Use( activator, caller, useType, val)
activator:SetHealth((math.Clamp( activator:Health() + 75, 100 )), 1, 100); – Sets the health as you were before, but makes it so it won’t go above 100 or below 1. If you want the other way just change it.
self:Remove(); – Removes it on use regardless.
end[/lua]

[lua]function ENT:Use( activator, caller, useType, val)
activator:SetHealth((math.Clamp( activator:Health() + 75, 100 )), 1, 100); – Sets the health as you were before, but makes it so it won’t go above 100 or below 1. If you want the other way just change it.
self:Remove(); – Removes it on use regardless.
end[/lua]

I don’t think you understand.

I used your code and it still didn’t remove itself even though, “Removes it on use regardless”.

[lua]function ENT:AcceptInput(name, activator, caller)
activator:SetHealth((math.Clamp( activator:Health() + 75, 100 )), 1, 100);
self:Remove();
end[/lua]

I do understand, as it works on any end but yours it seems.

Try that, I prefer that function.

As it not being removed, do you have it client?

Where is it located? Does the function call itself? If it’s not being removed you screwed something up.

I see, so it’s working differently for me.

Path:
garrysmod\addons\Custom Entities\lua\entities\medkit\init.lua




AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()

	self.damage = 15
	self:SetModel( "models/props_c17/BriefCase001a.mdl" )
	self:SetMaterial( "models/props_debris/plasterwall040c" )
	self:SetColor( Color(5, 105, 0, 255) )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )

	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
	phys:SetMass( 200 )
	phys:Wake()
	end

end

function ENT:AcceptInput(name, activator, caller)
	activator:SetHealth((math.Clamp( activator:Health() + 75, 100 )), 1, 100);
	self:Remove();
end

function ENT:OnTakeDamage( dmg )

self.damage = self.damage + dmg:GetDamage()

if self.damage <= 0 then self:Remove() end

end

function ENT:Touch( ent )

	if ent:IsOnFire() then
	self:Ignite(4,0)
	timer.Simple(4, function()
	self:Remove()
	end)

	end

end



I probably did screwed up somewhere, but the code looks fine to me.

Does it actually set their health? Let me see your function ENT:Initialize()

Yes, it sets the health repeatedly until it becomes 100.

The self:Remove() inside the other functions work.

Could it be something externally? I am running on a custom gamemode with hardly anything in it, I don’t think this matters though.

Post your ENT:Initialize()

BTW, I’ve posted the entire init.lua above.




function ENT:Initialize()

	self.damage = 15
	self:SetModel( "models/props_c17/BriefCase001a.mdl" )
	self:SetMaterial( "models/props_debris/plasterwall040c" )
	self:SetColor( Color(5, 105, 0, 255) )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )

	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
	phys:SetMass( 200 )
	phys:Wake()
	end

end



Did not notice cause was edit.

Add this: self:SetUseType( SIMPLE_USE )

Probably won’t do anything, but it’s the only thing else I can think of.