Jump to content

How do I make my character change stats depending on hp?

Recommended Posts

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


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
    elseif hp < 25
    elseif hp < 50
    elseif hp < 100
    else -- >= 100

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...