Jump to content

[SOLVED] How to make character not hold items with specific tags?


Recommended Posts

I am making a modded character that I want to refuse equipping hand slot items that don't fit the criteria. Still, I'm having some trouble making the item the player wanted to equip safely go back into their inventory. Here's what the code in my character's master_postinit looks like that is responsible 
 

local old_Equip = inst.components.inventory.Equip
		inst.components.inventory.Equip = function(self, item, ...)
			if item.components.equippable.equipslot == EQUIPSLOTS.HANDS and item:HasTag("weapon") and not item:HasTag("tool") then
			inst.components.talker:Say(GetString(inst, "ANNOUNCE_NO_WEAPON_FOR_ME_THANKS"))
			
			inst.puff = SpawnPrefab(item)
			inst.components.inventory:GiveItem(inst.puff)
			item:DoTaskInTime(0, item.Remove)
			return false
		end
		return old_Equip(self, item, ...)
	end

Also, whenever I equip an item that triggers this function to happen, the specific line of code that causes the game to crash is "inst.puff = SpawnPrefab(item)"

Edited by Marmalade
solved
Link to comment
Share on other sites

I soon realized that items with components and stats like durability and skins would be completely reset, getting set back to their max durability and default skin when they were spawned, so I created a different method that keeps these the same

New code:

local old_Equip = inst.components.inventory.Equip
		inst.components.inventory.Equip = function(self, item, ...)
			if item.components.equippable.equipslot == EQUIPSLOTS.HANDS and item:HasTag("weapon") and not item:HasTag("tool") then
			inst.components.talker:Say(GetString(inst, "ANNOUNCE_NO_WEAPON_FOR_ME_THANKS"))
			
			self:DropItem(item)
			self:GiveItem(item)
			
			return false
		end
		return old_Equip(self, item, ...)
	end

 

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