• Glitch or programming error with SetHealth entity?
    34 replies, posted
This doesn't remove itself unless I disable 'if ply:Health() >= 100 then return end', but why? It seems like the code reloops itself. [CODE]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[/CODE]
Because return keyvalue prevents any further code execution.
I rewrote it like this and still the same effect. [CODE]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[/CODE]
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: [code] function ENT:Use( ply ) if ply:Health() < 100 then ply:SetHealth( math.min( ply:Health() + 75, 100 ) ) self:Remove() end end[/code]
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] [IMG]http://i.imgur.com/r5KwmNf.png[/IMG]
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. [IMG]http://i.imgur.com/a0AVjUr.png[/IMG]
Well, as I said bunch of posts earlier... [QUOTE=Robotboy655;44805440][B]It will not remove itself if players health is less or equal to 100[/B]. That's what it is coded to do.[/QUOTE] Please, PAY ATTENTION.
[code] function ENT:Use( ply ) if ply:Health() < 100 then ply:SetHealth( math.min( ply:Health() + 75, 100 ) ) self:Remove() end end[/code] Is this suppose to remove the entity? Because it doesn't.
[QUOTE=Winter;44810290][code] function ENT:Use( ply ) if ply:Health() < 100 then ply:SetHealth( math.min( ply:Health() + 75, 100 ) ) self:Remove() end end[/code] Is this suppose to remove the entity? Because it doesn't.[/QUOTE] 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 [CODE] 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 [/CODE] 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. [CODE] 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 [/CODE]
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.
Nope, didn't work. I appreciate the help though.
Does it remove under any other parameters, such as its on fire?
[QUOTE=Winter;44810921]Nope, didn't work. I appreciate the help though.[/QUOTE] Ok let's start over. Can you get back to using Use instead of AcceptInput, and remove everything that isn't strictly necessary such as the OnTakeDamage and Touch functions? Then try it, post your entire code, and tell exactly what the issue is.
If I am reading correctly you want it to heal the players health + 75, and you want it to not go over 100. [code] local HealthGain = 75 -- No need to define this every time its used function ENT:Use(ply) if ply:Health() + HealthGain > 100 then -- Checks if the players health + 75 will be greater than 100 ply:SetHealth(100) -- and if so sets it to 100 else ply:SetHealth(ply:Health()+HealthGain) -- otherwise it will just add 75 to the health end self:Remove() -- and be removed once it's used end [/code] edit: Fixed a spelling error
_Jacob, I suggest you take the time to read the previous posts.
All you had to do is move the self:Remove() outside of the statement block. [lua] function ENT:Use(ply) if ply:Health() < 100 then ply:SetHealth(math.min(ply:Health() + 75, 100)) end self:Remove() end [/lua] You should be setting the [URL="http://wiki.garrysmod.com/page/Entity/SetUseType"]usetype[/URL] of the entity in your initialize. Mark this as solved.
The problem has to be external. I don't see why this won't work, it's very basic. I just wanted to see if anyone else had this result, but somehow I'm the only one. The problem is, if I use the IF condition, 'if health is less than 100', it will not remove itself and as a result it will keep adding health until 100. If I remove the IF statement, it will give me +100 health and then remove itself. I'm trying to simply add +75 health and remove itself if health is less than 100. [LUA] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize() --self.damage = 15 self:SetUseType( SIMPLE_USE ) 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 ) self:SetUseType( SIMPLE_USE ) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:SetMass( 200 ) phys:Wake() end end function ENT:Use( ply ) if ply:Health() < 100 then ply:SetHealth( math.Clamp(ply:Health() + 75, 1, 100) ) self:Remove() end end [/LUA] Edit: I just added self:SetUseType( SIMPLE_USE ), still the same.
[QUOTE=Winter;44818704]The problem has to be external. I don't see why this won't work, it's very basic. I just wanted to see if anyone else had this result, but somehow I'm the only one. The problem is, if I use the IF condition, 'if health is less than 100', it will not remove itself and as a result it will keep adding health until 100. If I remove the IF statement, it will give me +100 health and then remove itself. I'm trying to simply add +75 health and remove itself if health is less than 100. [LUA] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize() --self.damage = 15 self:SetUseType( SIMPLE_USE ) 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 ) self:SetUseType( SIMPLE_USE ) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:SetMass( 200 ) phys:Wake() end end function ENT:Use( ply ) if ply:Health() < 100 then ply:SetHealth( math.Clamp(ply:Health() + 75, 1, 100) ) self:Remove() end end [/LUA] Edit: I just added self:SetUseType( SIMPLE_USE ), still the same.[/QUOTE] You still have the exact same Use function as before. Instead of just skimming through a post you should actually read it because many people have given you the answer.
You really are adorable. We've pointed this issue out, did you learn to read? Take self:Remove() out of the code block. Also don't use my math.Clamp, it was more complicated than the other one. Use the math.min posted earlier back.
[QUOTE=Nookyava;44819026]You really are adorable. We've pointed this issue out, did you learn to read? [/QUOTE] Oh, you're acting quite cute yourself. Trying to point out some bullshit that hasn't worked for my computer and explaining to me that if I simply remove the Remove() function everything will work great. If I did what you said it [B]won't[/B] remove itself at all. You own the math.Clamp() function now? If you haven't noticed I refined it from scratch, it's not your "math.Clamp" it's mine. .
Sorry, you need to Log In to post a reply to this thread.