Jump to content

Recommended Posts

Hey guys!

 

So I was trying to make a script with a hat that does a thing when you unequip it. Like if you unequip your hat your sanity decreases.... (I used the hat template btw) also I tried to limit the effect so it occurs only if a certain character tries to unequip it. I'm pretty sure you can do it with an listenforevent and it would be much easier but I don't know how that works...soo a little help would be awesome because right now it only produce errors and crashes

 

 

local function OnEquip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_hat", "hat_bee_bw_swap", "swap_hat")

    owner.AnimState:Show("HAT")
    owner.AnimState:Show("HAT_HAIR")
    owner.AnimState:Hide("HAIR_NOHAT")
    owner.AnimState:Hide("HAIR")

    inst.hatisweared= true

    if owner:HasTag("player") then
        owner.AnimState:Hide("HEAD")
        owner.AnimState:Show("HEAD_HAT")
    end
end

local function OnUnequip(inst, owner)
    owner.AnimState:Hide("HAT")
    owner.AnimState:Hide("HAT_HAIR")
    owner.AnimState:Show("HAIR_NOHAT")
    owner.AnimState:Show("HAIR")

    inst.hatisweared= false

    if owner:HasTag("player") then
    inst.components.sanity.dapperness = TUNING.DAPPERNESS_WILSON*-3
        owner.AnimState:Show("HEAD")
        owner.AnimState:Hide("HEAD_HAT")
        
    end
end

local function isitequiped(inst)
if inst.hatisweared= True and owner:HasTag("goodguy") then
owner.components.sanity.dapperness = -TUNING.DAPPERNESS_HUGE
else
owner.components.sanity.dapperness = TUNING.DAPPERNESS_HUGE
end
end

 

@YordlePrincess, you're doing it wrong entirely. inst is the instance of the hat not the character. owner is the instance of the character.

local function OnUnequip(inst, owner)    owner.AnimState:Hide("HAT")    owner.AnimState:Hide("HAT_HAIR")    owner.AnimState:Show("HAIR_NOHAT")    owner.AnimState:Show("HAIR")    inst.hatisweared= false    if owner:HasTag("player") then        owner.components.sanity:DoDelta( -10 )        owner.AnimState:Show("HEAD")        owner.AnimState:Hide("HEAD_HAT")            endend 

I see, thanks :) I have another question if you could help with that too

 inst:ListenForEvent("onpickupitem", function(inst, data)        if data.item.prefab == "flower" then            inst.components.sanity:DoDelta(-TUNING.SANITY_LARGE)        end    end)

I have this script...I've tried it many times...it works well with any other object like logs and stuff but it doesn't work with flowers for some reason it completely ignores it actually...no crash or anything but picking up flowers still give you positive sanity...just can't understand why?

 

 

@YordlePrincess, you're doing it wrong entirely. inst is the instance of the hat not the character. owner is the instance of the character.

local function OnUnequip(inst, owner)    owner.AnimState:Hide("HAT")    owner.AnimState:Hide("HAT_HAIR")    owner.AnimState:Show("HAIR_NOHAT")    owner.AnimState:Show("HAIR")    inst.hatisweared= false    if owner:HasTag("player") then        owner.components.sanity:DoDelta( -10 )        owner.AnimState:Show("HEAD")        owner.AnimState:Hide("HEAD_HAT")            endend 

 

@YordlePrincess, if you're trying to modify the amount of sanity gained from flower it is better to AddPrefabPostInit the flower prefab and modify the data that way. 

 

Something like this?

AddPrefabPostInit("flower", function(inst)    inst:ListenForEvent("onpickupitem", function(inst, data)        if data.item.prefab == "flower" then            inst.components.sanity:DoDelta(-TUNING.SANITY_LARGE)        end		    endend)

@YordlePrincess, more like the following:

AddPrefabPostInit("flower", function(inst)    local _onpickedfn = inst.components.pickable.onpickedfn    inst.components.pickable.onpickedfn = function( inst, picker)        if picker and picker.components.sanity and picker.prefab == "YOURCHARACTER" then            picker.components.sanity:DoDelta(-TUNING.SANITY_TINY)            picker.components.sanity:DoDelta(-TUNING.SANITY_LARGE)            _onpickedfn( inst, picker )        else            _onpickedfn( inst, picker )        end    endend) 

 The first is to remove what picking the flower added. The second delta call is to remove the sanity drain.

Edited by Kzisor

Thanks for the help it works..or at least the sanity thingy but when I pick up a flower it doesn't disappear now...I can "examine it only",  a couple seconds and then I can pick it up again

@YordlePrincess, more like the following:

AddPrefabPostInit("flower", function(inst)    local _onpickedfn = inst.components.pickable.onpickedfn    inst.components.pickable.onpickedfn = function( inst, picker)        if picker and picker.components.sanity and picker.prefab == "YOURCHARACTER" then            picker.components.sanity:DoDelta(-TUNING.SANITY_LARGE)        else            _onpickedfn( inst, picker )        end    endend) 

 The first is to remove what picking the flower added. The second delta call is to remove the sanity drain.

 

Thanks for the help it works..or at least the sanity thingy but when I pick up a flower it doesn't disappear now...I can "examine it only",  a couple seconds and then I can pick it up again

Sorry to say that but I have an issue with this script now...I wanted to try out with my friend and while I tried it during the making it worked fine, but when I tried to use him on a lan game the client crashed everytime with this: 

log.txt

@YordlePrincess, components should only run on the server.

AddPrefabPostInit("flower", function(inst)    if not TheWorld.ismastersim then        return inst    end    local _onpickedfn = inst.components.pickable.onpickedfn     inst.components.pickable.onpickedfn = function( inst, picker)        if picker and picker.components.sanity and picker.prefab == "YOURCHARACTER" then            picker.components.sanity:DoDelta(-TUNING.SANITY_TINY)            picker.components.sanity:DoDelta(-TUNING.SANITY_LARGE)            _onpickedfn( inst, picker )        else            _onpickedfn( inst, picker )        end     endend)  

AddPrefabPostInit("flower", function(inst)

if not GLOBAL.TheWorld.ismastersim then

return

end

local _onpickedfn = inst.components.pickable.onpickedfn

inst.components.pickable.onpickedfn = function( inst, picker)

if picker and picker.components.sanity and picker.prefab == "YOURCHARACTER" then

picker.components.sanity:DoDelta(-TUNING.SANITY_LARGE)

else

_onpickedfn( inst, picker )

end

end

end)

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