Jump to content

Drain sanity based on another player's health?


Recommended Posts

I want to have my character's sanity drain when players near them have low health or sanity. Any idea how to do this? Here's what I have in master_postinit.

Quote

    inst:DoPeriodicTask(1.0, function(inst)
        -- Do nothing if the player is dead.
        if inst.components.health:IsDead() or inst:HasTag("playerghost") then
            return
        end
        
        -- Store the position of the player in x, y, z variables.
        local x,y,z = inst.Transform:GetWorldPosition()
        
        -- Description of important function, which finds specific entities within a range:
        -- TheSim:FindEntities(x, y, z, radius, mustHaveTags, cantHaveTags, mustHaveOneOfTheseTags)
        
        -- We have limited it to any player that is not a ghost or in limbo.
        -- I have set the radius to be the one used for standard negative auras. You can set it to whatever radius you want.
        local ents = TheSim:FindEntities(x, y, z, TUNING.SANITY_EFFECT_RANGE, {"player"}, {"playerghost", "INLIMBO"}, nil)

        for i, v in ipairs(ents) do
            if v and v:IsValid() then -- If any player near them has low HP, drain sanity
                local health_percent = {"player"}.components.health:GetPercent() -- Player health
                local sanity_percent = {"player"}.components.sanity:GetPercent() -- Player sanity

                if (health_percent < .5 or sanity_percent < .5) then -- If player health or sanity is less than half
                     inst.components.sanity.dapperness = -1 -- Drain Nibs' sanity
                end

                if v.prefab == "bolt" then -- When near Bolt, gain sanity and do more damage
                    inst.components.sanity:DoDelta(1, true) -- Sanity gain
                    if not inst:HasTag("killmode") and inst.issecretnibs == nil and not inst:HasTag("playerghost") then -- If killmode is not already engaged
                        inst.components.combat.damagemultiplier = 1.5 -- Damage buff
                    end
                elseif v.prefab == "cobalt" then -- Sanity gain around Cobalt
                    inst.components.sanity:DoDelta(1, true) -- "true" disables the pulse on the badge and disables that it plays a sound.
                end
            elseif v and not v:IsValid() then    -- Turn off damage mod when not around Bolt
                inst.components.combat.damagemultiplier = 1
                inst.components.sanity.dapperness = 0
            end
        end
    end)

It's crashing when trying to grab the teammate health and sanity values.

Link to comment
Share on other sites

1 hour ago, localcryptid said:

                local health_percent = {"player"}.components.health:GetPercent() -- Player health
                local sanity_percent = {"player"}.components.sanity:GetPercent() -- Player sanity

Should be `v` instead of `{"player"}`.

You'll want to look over your logic after it, some of your if/then/elseifs don't look right at a glance for what you're intending to do.

Link to comment
Share on other sites

Quote

 

    inst:DoPeriodicTask(1.0, function(inst)
        -- Do nothing if the player is dead.
        if inst.components.health:IsDead() or inst:HasTag("playerghost") then
            return
        end
        
        -- Store the position of the player in x, y, z variables.
        local x,y,z = inst.Transform:GetWorldPosition()
        
        -- Description of important function, which finds specific entities within a range:
        -- TheSim:FindEntities(x, y, z, radius, mustHaveTags, cantHaveTags, mustHaveOneOfTheseTags)
        
        -- We have limited it to any player that is not a ghost or in limbo.
        -- I have set the radius to be the one used for standard negative auras. You can set it to whatever radius you want.
        local ents = TheSim:FindEntities(x, y, z, TUNING.SANITY_EFFECT_RANGE, {"player"}, {"playerghost", "INLIMBO"}, nil)

        for i, v in ipairs(ents) do
            if v and v:IsValid() then -- If any player near them has low HP, drain sanity
                inst.components.talker:Say("Near a player") 

                if (v.components.health:GetPercent() < .5 or v.components.sanity:GetPercent() < .5) then -- If player health or sanity is less than half
                     inst.components.sanity:DoDelta(-1, true) -- Drain Nibs' sanity
                     inst.components.talker:Say("Low HP/Sanity player, draining sanity") 
                end
                if v.prefab == "bolt" then -- When near Bolt, gain sanity and do more damage
                    inst.components.sanity:DoDelta(1, true) -- Sanity gain
                    inst.components.talker:Say("Bolt sanity gain") 
                    if not inst:HasTag("killmode") and inst.issecretnibs == nil and not inst:HasTag("playerghost") then -- If killmode is not already engaged
                        inst.components.talker:Say("Bolt damage mod") 
                        inst.components.combat.damagemultiplier = 1.5 -- Damage buff
                    end
                elseif v.prefab == "cobalt" then -- Sanity gain around Cobalt
                    inst.components.talker:Say("Bolt sanity gain") 
                    inst.components.sanity:DoDelta(1, true) -- "true" disables the pulse on the badge and disables that it plays a sound.
                end
            elseif v and not v:IsValid() then    -- Turn off damage mod when not around Bolt
                if not inst:HasTag("killmode") and inst.issecretnibs == nil and not inst:HasTag("playerghost") then -- If killmode is not already engaged
                    inst.components.talker:Say("Not near Bolt, damage multiplier reset") 
                    inst.components.combat.damagemultiplier = 1
                end
            end        
        end
    end)

 

Added a few print statements. Turns out "Near a player" is printing at all times now. 

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...