Hello guys, i have a trouble when i make a script that use 2 wood and 1 iron and my inventory have 5 wood and 10 iron after i clicked to craft all my items are gone do you guys know how to fix it? like set amount that will be remove after click i don’t know .
I want to help but currently its impossible since i dont know how your crafting script works. I would appreciate it if you would copy and paste the script so i can take a look and maybe help solve the issue (:
Ok here an example
Item = Backpack[wood]
Item2 = Backpack[wood]
On mouse click ()
Item:remove ()
Item2:remove()
Here a problem when the items remove all wood that in backpack are gone I want it to delete only 2 wood from my backpack sorry for my english
Well… i still dont know exaclly how your script looks
I decided to create this module script which should hopefully solve your issue, Its not perfect but it gets the job done.
How to use this module script:
local CraftModule = require(Path.To.CraftModule) -- put the actual path to your module
local recepie = {
["Wood"] = 2,
["Iron"] = 1
-- you can add any items you want
}
CraftModule.Craft(yourPlayer, recepie, someRewardItem) -- handles everything
CraftModule:
(I would recommend putting your module script somewhere in ServerScriptService)
local CraftModule = {}
function CraftModule.Craft(player, recepie, craftResult) -- Reruns true if the craft was successfull
local itemDictionary = FindItems(player, recepie)
if CanCraft(itemDictionary, recepie) then
RemoveItems(itemDictionary) -- destroy all of the necessary items
local resultItem = craftResult:Clone() -- clone and add the craftResult to the player backpack
resultItem.Parent = player.Backpack
print("Successfully crafted!")
return true
else
print("Not enought items to craft!")
return false
end
end
function FindItems(player, recepie) -- Retuns all the items that were in the player backpack matching the recepie
local itemDictionary = {}
for i, item in ipairs(player.Backpack:GetChildren()) do -- loop over the backpack
local amount = recepie[item.Name]
if item:IsA("Tool") and amount then -- if the recepie contains the item name
itemDictionary[item.Name] = itemDictionary[item.Name] or {} -- if itemDictionary[item.Name] is nill then create a new table for it
if #itemDictionary[item.Name] < amount then -- if the amount of found items with this name is smaller than the needed amount then...
table.insert(itemDictionary[item.Name], item) -- add this item to the table
end
end
end
return itemDictionary
end
function CanCraft(itemDictionary, recepie) -- Returns true if there are enought items for the craft to happen
for name, amount in pairs(recepie) do -- loop over the recepie
if not itemDictionary[name] or #itemDictionary[name] < amount then -- if there are not enought items with this name then return false
return false
end
end
return true -- if all of the items match then return true
end
function RemoveItems(itemDictionary) -- Destroys all of the given items
for k, items in pairs(itemDictionary) do
for i, item in ipairs(items) do
item:Destroy()
end
end
end
return CraftModule
Of course you can change this module script however you want but this can be a good starting point. I hope this helps!
Thanks you this help me alot hope your carrer going great brother!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.