Jump to content

[SOLVED] How to drop a specific held item in ModMain?


Recommended Posts

Hello, 

I am trying to get my character to unequip a specific item when they change forms. The code that I use works fine when unequipping all items in that specific slot, but when I ask for a specific prefab (my character's weapon) - I get the "attempt to index local 'handitem' (a nil value)" error.

The "handitem.prefab" is where it's failing to fire.

local function WearHood(inst)
if not inst:HasTag("playerghost") then
    if inst:HasTag("IsNotHooded") then ---Activate hood
		local headitem = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)
		local handitem = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
		AddPrefabPostInit("wakes_bow", function(inst) end)
		if inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD) then
			inst.components.inventory:Unequip(GLOBAL.EQUIPSLOTS.HEAD)
			inst.components.inventory:GiveItem(headitem)
			inst.AnimState:OverrideSymbol("swap_hat", "nil", "nil")
		end
		if handitem.prefab == "wakes_bow" then
			inst.components.inventory:Unequip(GLOBAL.EQUIPSLOTS.HANDS)
			inst.components.inventory:GiveItem(handitem)
			inst.AnimState:OverrideSymbol("swap_object", "nil", "nil")
		end
		inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_close", "open")
		inst.AnimState:SetBuild("wakes_hood")
		inst.components.temperature.inherentinsulation = (60)
		inst.components.health:SetAbsorptionAmount(0.50)
		inst:RemoveTag("IsNotHooded")	
	elseif not inst:HasTag("IsNotHooded") then ---Deactivate hood
		inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_open", "open")
		if inst.components.beard.bits == 0 then
			inst.AnimState:SetBuild("wakes")
		elseif inst.components.beard.bits == 6 then
			inst.AnimState:SetBuild("wakes_short")
		elseif inst.components.beard.bits == 10 then
			inst.AnimState:SetBuild("wakes_med")
		elseif inst.components.beard.bits == 16 then
		inst.AnimState:SetBuild("wakes_long")
		end
		inst.components.temperature.inherentinsulation = (0)
		inst.components.health:SetAbsorptionAmount(0)
		inst:AddTag("IsNotHooded")
		end
    end
	end

 

Edited by Scrumch
Link to comment
Share on other sites

It's possible that the function is being run when nothing is in the hand slot, so when it checks for handitem.prefab for the first time it won't find anything. Maybe you should put "if handitem then" before you use handitem...

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