Left 4 Sharkb8 Posted January 1, 2022 Share Posted January 1, 2022 I'm trying to make a character who gets stronger as they lose hp, with them getting buffed at less than 100, 50, 25, and 2 hp, but the game straight up ignores it! I don't know how to solve this issue. I've tried using healthdelta, which is what Walter's code uses to listen for health change, but nothing happened. I've also tried using the onhealthchange function from Wanda's code, and nothing really changed. After that I swapped some stuff around and now I'm getting the error "attempt to compare number with nil", when all the code does for the line it mentions in the error is just check to see if the player has more than 100 hp. I've attached my code and the server log below, in case you want to help me out here. (also I'm sure my code is horribly optimized, sorry in advance lol) derpzones.lua server_log.txt Link to comment Share on other sites More sharing options...
CarlZalph Posted January 2, 2022 Share Posted January 2, 2022 @logie757 There's a few things wrong with the logic and handling of events I'm seeing on a cursory glance. In LUA doing `not variable` is true if and only if the value of variable is nil or false. Since you're using it with a variable that points to a function it will always return false. First, you're trying to use `.current` for the health component which uses `.currenthealth` for its variable name. Then there's the cascade optimization you could do for putting the range into bins: local hp = inst.components.health.currenthealth if hp < 2 then ExtremelyLowHealthBuff(inst) elseif hp < 25 then VeryLowHealthBuff(inst) elseif hp < 50 then LowHealthBuff(inst) elseif hp < 100 then MediumHealthBuff(inst) else -- >= 100 ResetBuffs(inst) end And to check it so you only apply the buff once, use some variable attached to the entity itself with a unique namespace like: local function MediumHealthBuff(inst) if inst.derpzones_buff == 1 then return end inst.derpzones_buff = 1 With a unique value for each of the functions. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now