• HELP! Self Health is going over maxhealth!
    66 replies, posted
util.AddNetworkString( "SetHealth1" ) net.Receive( "SetHealth1", function( bits, ply )local maxhealth = ply:GetMaxHealth() or 100 if ply:Health() < maxhealth then ply:SetHealth(ply:Health() + 25) end end) util.AddNetworkString( "SetHealth2" ) net.Receive( "SetHealth2", function( bits, ply )local maxhealth = ply:GetMaxHealth() or 100if ply:Health() < maxhealth then ply:SetHealth(ply:Health() + 50) end end) util.AddNetworkString( "SetHealth3" ) net.Receive( "SetHealth3", function( bits, ply )local maxhealth = ply:GetMaxHealth() or 100if ply:Health() < maxhealth then ply:SetHealth(ply:Health() + 75) end end) util.AddNetworkString( "SetHealth4" ) net.Receive( "SetHealth4", function( bits, ply )local maxhealth = ply:GetMaxHealth() or 100 if ply:Health() < maxhealth then ply:SetHealth(ply:Health() + 100) end end)
wtf
Ok listen. I want you to learn. Why don't you try just reading out loud each line of the code, each function call to yourself and think about it? (aka Rubber duck debugging) It's really not that hard to understand why that is happening and how to fix.
why?
It's ok, use the wiki for functions you don't know or don't understand. http://wiki.garrysmod.com/page/Main_Page net.Receive( "SetHealth4", function( bits, ply ) local maxhealth = ply:GetMaxHealth() or 100 if ply:Health() < maxhealth then ply:SetHealth(ply:Health() + 100) end end) I want you to just type out what this does line by line. Don't be afraid, ask any questions except for "why it doesn't work", I want you to come to that conclusion yourself and learn from it.
https://i.gyazo.com/a8ea14c13f5159245d8e4e04bc8599f9.png Btw, could you please explain in more detail what exactly are you trying to do here? Also, please use https://pastebin.com/
So Rubat put the code in lines how i did in the lua file the problem is with the code, when i under 100 hp heal me then my hp is gonna be over the maxhp thats the problem and i want help to find the correct code that the heal do not get over the maxhp
How about setting the health to maxhp if health > maxhp?
Ailright i tried and i nothing happend just dont give me anymore the health
Do you just want to set the health to the maximum health? or specific amounts?
i already done with how much health i want to get but the problem is when i am under 100 health and heal myself its gonna be over 100 hp and what i want is that when its hitting 100 hp thats should not go over the 100 hp
math.Clamp(0, place_health_here, 100)
so the thing now is i want that to have for when i give myself + 75, + 50 and + 25
Then do it??? He gave you the line to do what you want...
Are you even trying?
i tried works kinda but like when i want to give me 75 hp i want like it should add 75 hp but yeah when its hit 100 hp it should not go over the 100 hp
Help me help you
So what you gave me works but not what i wanted to expect what i expect is whem you give yourself like lets say 25 hp yeah? That should add to your hp but the bug was from the code what i had and not your code is when i was hitting the 100 hp it was going over the 100 hp like lets say i got 80 hpe it give me 105 hp and not 100 hp
So what you're saying is that it works now?
Think of the code you have now literally, it might sound something like this: "When my current health is less than 100, I want to add 75 Health to my current health, which is 50, so my new health after the heal will be 125." Remember that code will be called in order, that means that the code that checks the players health is checked right before you heal them, not at the same time.
exactly thank you!
Fancy fixed it for u ply:SetHealth(math.Clamp(0, place_health_here, max_health)) math.Clamp Click this to learn how to use this function. To briefly explain why you would use this function to solve your problem, it returns a value no higher or lower than the values you give it. This means that if you feed it the max amount of hp someone can have and then feed it a value that goes over that number, it will "clamp" at the max value. I think I understood where you were currently at with the problem? If I didn't, sorry.
Now nothing is working for me right now i just saying I am making a MedKit Entity thats when you use it like pressing e. It opens a DFrame menu and that you get 4 difrrent health regeneration like give u self 25, 50, 75 and 100
Didnt i already showed you a easy way of how to make the player not overheal over 100? Now your right back with the old code that made the player overheal
No no i put your code
Button4.DoClick = function( self ) ply:SetHealth( math.Clamp( ply:Health() + 100, 0, 100 ) ) end it should work with math.Clamp..you give the players health + 100 for a 100 healing, min giving 0, max giving 100.. oh wait. You dont check anymore if the players health is less than the 100 or whatever your healing is. So whats happening is, math.Clamp DOES limit the healing to 100, but that doesnt help you at all because it can still add 100 health to the player, even if his health is 99. so we could get 199 health. The math.clamp does not help you on overhealing. You WILL need a if statement for that. The one i showed you
math.Clamp check the example 2 pls
The example does not work the way you think it does. The 2 last numbers you give ply:SetHealth( math.Clamp( ply:Health() + 100, 0, 100 ) ) 0, 100. They only limit the health number you wanna ADD. but they do not care about the health the player already has. If the player has 99 health, the math.clamp will add 100 still. So the player has 199. So the math.Clamp is actually completley useless, yo ucan still just do ply:SetHealth(ply:Health() + 100) you WILL still need the IF statement that i showed you earlier or 2 days ago or so.. Its not possible without it. And definetly not possible with only math.Clamp I know what you tryd to do, you thought math.Clamp clamps the health the player will get, you thought the last number, the 100, will be the max health of the player, but it isnt. its the max number of health the player will get, no matter how many health he already has.
local Button4 = vgui.Create( "DButton", DermaPanel )     Button4:SetText( "100%" ) Button4:SetPos( 10, 185 ) Button4:SetSize( 230, 50 ) Button4.DoClick = function( self )     ply:SetHealth(ply:Health() + 100) end Still dont give me the heal
You're running the code on the client
Sorry, you need to Log In to post a reply to this thread.