YordlePrincess Posted July 13, 2015 Share Posted July 13, 2015 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") endendlocal 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") endendlocal function isitequiped(inst)if inst.hatisweared= True and owner:HasTag("goodguy") thenowner.components.sanity.dapperness = -TUNING.DAPPERNESS_HUGEelseowner.components.sanity.dapperness = TUNING.DAPPERNESS_HUGEendend Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/ Share on other sites More sharing options...
Kzisor Posted July 13, 2015 Share Posted July 13, 2015 @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 Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654049 Share on other sites More sharing options...
YordlePrincess Posted July 14, 2015 Author Share Posted July 14, 2015 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 Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654337 Share on other sites More sharing options...
Kzisor Posted July 14, 2015 Share Posted July 14, 2015 @YordlePrincess, try petals. Flowers become petals in your inventory. Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654346 Share on other sites More sharing options...
YordlePrincess Posted July 14, 2015 Author Share Posted July 14, 2015 It works if I drop the petals and try to pick them up but doesn't do anything when I pick up a flower :/@YordlePrincess, try petals. Flowers become petals in your inventory. Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654350 Share on other sites More sharing options...
Kzisor Posted July 14, 2015 Share Posted July 14, 2015 @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. Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654355 Share on other sites More sharing options...
YordlePrincess Posted July 14, 2015 Author Share Posted July 14, 2015 @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) Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654362 Share on other sites More sharing options...
Kzisor Posted July 14, 2015 Share Posted July 14, 2015 (edited) @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 July 15, 2015 by Kzisor Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654364 Share on other sites More sharing options...
YordlePrincess Posted July 15, 2015 Author Share Posted July 15, 2015 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. Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654636 Share on other sites More sharing options...
Kzisor Posted July 15, 2015 Share Posted July 15, 2015 (edited) @YordlePrincess, sorry, it looks like some of the code wasn't added correctly. I modified the original code. Edited July 15, 2015 by Kzisor Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654639 Share on other sites More sharing options...
YordlePrincess Posted July 15, 2015 Author Share Posted July 15, 2015 It works perfectly now thanks a lot you have a lot of patience ^^@YordlePrincess, sorry, it looks like some of the code wasn't added correctly. I modified the original code. Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-654681 Share on other sites More sharing options...
YordlePrincess Posted July 20, 2015 Author Share Posted July 20, 2015 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 againSorry 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 Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-655981 Share on other sites More sharing options...
Kzisor Posted July 20, 2015 Share Posted July 20, 2015 @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) Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-655983 Share on other sites More sharing options...
DarkXero Posted July 20, 2015 Share Posted July 20, 2015 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) Link to comment https://forums.kleientertainment.com/forums/topic/56142-hat-that-does-a-thing-when-equiped/#findComment-655985 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now