Jump to content

Character Mod - Increase Speed as Health Lowers (Help I'm Trash)


Recommended Posts

So I'm trying to make a custom character just for fun and I want a perk/trait thingo to be when his health goes lower his walk/run speed increases. I've pasted what I have done below and have also linked the whole charactername.lua file if you wanted to look at the whole thing.

I'm no good at coding or instructions and couldn't find exactly what I needed on other forums, I tried to look at another mod where the speed lowered as the sanity lowered and tried to switch it around to what I need but didn't get it working. This is what I've put in and I'm not sure what I've done wrong/ missed out: 

-- Speed Increases as Health Lowers
local function sanitycheck(inst)
local healthcurr = inst.components.health.currenthealth

    if healthcurr >= 200 then
        inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 0.85)
        inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 0.95)
    elseif healthcurr >= 150 then
        inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1)
        inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.10)
    elseif healthcurr >= 100 then
        inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.15)
        inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.25)
    elseif healthcurr >= 60 then 
        inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.25)
        inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.35)
    else 
        inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.5)
        inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.6)
    end
end

Any and all help is appreciated, thanks :)

gud.lua

Link to comment
Share on other sites

Trust me Doing this is hell, I alreaddy did it on my character who bases around lifesteal & health gain but you'll prefer to play low hp for the speed

=======================


local CHEALTH = inst.components.health.currenthealth
local MHEALTH = inst.components.health.maxhealth

local SPEEDINCREASE = 1 -- SET YOUR SPEEDINCREASE HERE, How much speed you gain upon having less health

local BASESPEED = 1 --  SET YOUR BASESPEED HERE, which means this speed will always be added to prevent you from being frozen when max hp.

-- MAXSPEED IS EQUAL TO BASESPEED + SPEEDINCREASE

-- EXAMPLE max hp 100 current hp 50 (((100-50)/(100/6))+2) = 5 speed
inst.components.locomotor.walkspeed =  math.ceil (((MHEALTH - CHEALTH)/(MHEALTH/SPEEDINCREASE))+BASESPEED)
inst.components.locomotor.runspeed = math.ceil (((MHEALTH - CHEALTH)/(MHEALTH/SPEEDINCREASE))+BASESPEED)

-- EDIT; I do not know wilsons base speed :( but I think it's around 6 or something

Edited by Bosoca
Link to comment
Share on other sites

The function has never been called.

inst:ListenForEvent("sanitydelta", sanitycheck)

add this into onload. And move sanitycheck ahead of onload.

 

Edited by ptr
Link to comment
Share on other sites

9 hours ago, ptr said:

The function has never been called.


inst:ListenForEvent("sanitydelta", sanitycheck)

add this into onload. And move sanitycheck ahead of onload.

 

Sorry this is my first time I understand nothing, but would it not be ("healthdelta", healthcheck) since it's based on health not sanity?

Link to comment
Share on other sites

I have managed to get it half working, so at the health variables >= 100, >=75, >=55 and >=40, the player's speed is increased however once the health reaches below 40, the game crashes and comes up with this crash log: image.thumb.png.0ba25679dd81e9a392678f73949beaeb.png

Line 61 is

elseif inst.components.currenthealth >= 25 then

Not sure why it is only occurring to this line of code as the other health values are formatted in the exact same way.

The whole thing currently looks like this: 

-- Health modifiers
local function healthcheck(inst) 
local healthcurr = inst.components.health.currenthealth
   
    local attranges = 1

    if inst.components.health.currenthealth >= 100 then
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 0.85)
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 0.9)
    
    elseif inst.components.health.currenthealth >= 75 then
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1 )
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.05)
    
    elseif inst.components.health.currenthealth >= 55 then
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.15)
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.2)
    
    elseif inst.components.health.currenthealth >= 40 then
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.3)
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.35)
    
    elseif inst.components.currenthealth >= 25 then -- (the line that is causing crash according to log)
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.45)
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.5)   
    
    else
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 2)
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 2.05)
    end
end

 

Thanks

Link to comment
Share on other sites

1 hour ago, ptr said:

Well, the function name is sanitycheck, so I thought it was about sanity.

And that line should be

elseif inst.components.health.currenthealth >= 25 then

OML I am blind as hell, thanks heaps man :) No idea how I didn't notice that D; 

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
 Share

×
  • Create New...