Jump to content

[SOLVED] Making hat's shield/light only appear at low sanity?

Recommended Posts

I'm making a hat that has the shield of the Thulecite Crown. I have the shield working, but I'd like it to only be able to activate when the character wearing it is below 40% sanity.

I tried making a ListenForEvent for the wearer's sanitydelta that would run a function to enable and disable the shield, but this would cause the game to crash due to complications with inst.onattach / inst.ondetach.

I then got the idea to just set the probability of the shield triggering to 0 when the character wearing it was sane. However, I only know how to do this via changing the TUNING value, which predictably changes the probability of the shield triggering for everyone on the server. I just want it to change for the insane wearer.

If anyone could help me out with this, I'd appreciate it. Thanks for reading.


If it helps, I made a stripped down version of my prefab without the event listeners and sanity-checking function, using a template. I will be changing all of the "ruinshat" variables later, I just want the bloody thing to work first.


Edited by Chesed
i'm bad at coding
Link to comment
Share on other sites

Add 2 more if checks to your tryproc function that check whether the user is insane or not:

local function tryproc(inst, owner, data)
    if inst._task == nil and
        not data.redirected and
        math.random() < TUNING.ARMOR_RUINSHAT_PROC_CHANCE and
        owner.components.sanity and owner.components.sanity:GetPercent() < 0.4 then -- Check whether the wearer has the sanity component and whether their current sanity percent is lower than 40%
        ruinshat_proc(inst, owner)


  • Thanks 1
Link to comment
Share on other sites

Sorry to be a bother, but I have another issue with the same item and don't want to clog up the forum. I don't mind at all if you aren't able to help further.

I'm trying to make the hat light up when the wearer is insane, which works, but once the light gets triggered I can't get it to turn off again when it's supposed to. It stays even if the hat is removed.

I'm probably doing something extremely stupid but I'm comparing this light to the lights I've already made that are working as intended and I'm completely at a loss.

I've reproduced the bug on a stripped down version of the item. Please excuse my spaghetti code.


Link to comment
Share on other sites

The problem is you added the sanitydelta listener onto the player. The CheckLightSurface function that was supposed to run when players sanity is low has its first argument as owner (which in this case is in fact the player), however the second argument is called inst and used as if it is some sort of an entity that you try adding the light onto.

The second argument in this function is a table supplied with these values: 

oldpercent = self._oldpercent, newpercent = self:GetPercent(), overtime = overtime, sanitymode = self.mode (from sanity.lua line 307)

What you're essentially doing is adding the light to that table.

Replace your current CheckLightSurface with this and it should work:

local function CheckLightSurface(inst, data)
	if data.newpercent <= 0.15 then
        if inst._light == nil or not inst._light:IsValid() then
            inst._light = SpawnPrefab("shadowmasklight")


As well as change the argument for turnoff_shadowmask in line 50 from inst to owner.

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