nsplugins icon indicating copy to clipboard operation
nsplugins copied to clipboard

crafting system issue

Open pedroxvb opened this issue 7 years ago • 1 comments

when you craft something, that has multiple items, for example i want to do that :

local RECIPE = {}
RECIPE.uid = "example"
RECIPE.name = "A Skull"
RECIPE.category = nut.lang.Get( "icat_material" )
RECIPE.model = Model( "models/Gibs/HGIBS.mdl" )
RECIPE.desc = "A Skull."
--RECIPE.noBlueprint = true
RECIPE.items = {
	["bone"] = 2,
}
RECIPE.result = {
	["skull"] = 1,
}
RECIPES:Register( RECIPE )

It is actually set to use 2 bones to craft a skull, but it doesn't work, the craft is successfull even if i use only one bone. I mean if i use 2 bones they both disapear from my inventory and i got the skull but if i use only one bone it will give me the skull aswell and remove the single bone. I'm sorry for terrible explanation, i'm not very good at explaining things. Can you help me with this i don't know what i need to change in the code to make it properly work, i know i need to modify something in this area but i'm new to lua and i don't know how to fix this myself :

function RECIPES:Register( tbl )
	if !tbl.CanCraft then
		function tbl:CanCraft( player )
			for k, v in pairs( self.items ) do
				if !player:HasItem( k, v ) then
					return false
				end
			end
			return true
		end
	end
	if !tbl.ProcessCraftItems then
		function tbl:ProcessCraftItems( player )

			player:EmitSound( "items/ammo_pickup.wav" )
			for k, v in pairs( self.items ) do
				for i = 1, v do
					if player:getChar():getInv():hasItem( k ) then
						player:getChar():getInv():remove( player:getChar():getInv():hasItem( k ):getID() )
					end
				end
			end
			for k, v in pairs( self.result ) do
				--print(player:getChar():getInv():add(k, v))
				--if (!player:getChar():getInv():add(k, v)) then
					for i = 1, v do
					nut.item.spawn(k, player:getItemDropPos())
					end
				--else
					--netstream.Start(client, "vendorAdd", uniqueID)
				--end
			end
			player:notifyLocalized( "donecrafting", self.name )

		end
	end
	self.recipes[ tbl.uid ] = tbl
end

Hope it is clear, and thanks for any help by advance :).

pedroxvb avatar Nov 03 '17 00:11 pedroxvb

Hello I know I am almost a year late, but better late than never! I managed to fix this script with just a few small edits

for k, v in pairs( self.items ) do
	if player:HasItem( k, v ) then
		if player:getChar():getInv():getItemCount(k) < v then
			return false
		end
	end
	if !player:HasItem( k, v ) then
		return false
	end
end
return true

Stanstar22 avatar Sep 21 '18 14:09 Stanstar22