I’m trying to make a crafting system, but when trying to add stacking I got stuck, it outputs nothing, I tried fixing it but it broke even more. I’m not really good at coding, so you might wanna organize my code.
Issue
When I check the inventory tab in the gui, it only appears with a stick and thats all, this happened when I tried making the stacking system.
Plans for crafting system (Might be pointless)
To craft something, you will need to pick some resources first, and everytime something gets added, a localscript will loop through a modulescript with all the recipes, if it finds a recipe with the same recources, then it will put the tool the recipe makes in a box, and if you click the tool, it will give the player the tool, then remove the used resources (Example: 2 sticks + 3 stone = Stone Pickaxe)
local plr = game.Players.LocalPlayer
local inventory = plr:WaitForChild("Backpack")
local itemsList = {}
local stackList = {}
local template = script.Parent:WaitForChild("Tem")
function removeButton(child)
local target = script.Parent.Inventory.ScrollingFrame[child.Name]
if target then
target:Destroy()
end
end
function newButton(child)
local found = false
for i,v in pairs(itemsList) do
if v == child.Name then
if stackList[i] then
value = stackList[i]
table.remove(stackList,i)
table.insert(stackList, i, value+1)
else
table.insert(stackList,i,2)
end
local button = script.Parent.Inventory.ScrollingFrame:FindFirstChild(child.Name)
button.Text = child.Name.." ("..tostring(stackList[i])..")"
else
found = true
end
end
if found == false then
local newButton = template:Clone()
newButton.Parent = script.Parent.Inventory.ScrollingFrame
newButton.Name = child.Name
newButton.Text = child.Name
newButton.Visible = true
table.insert(itemsList,child.Name)
newButton.MouseButton1Up:Connect(function()
-- nothing for now
end)
end
end
wait(1)
for i,v in pairs(inventory:GetChildren()) do
newButton(v)
end
inventory.ChildAdded:Connect(newButton)
inventory.ChildRemoved:Connect(removeButton)
Here’s my explorer too. (I circled the broken script just incase of any confusion)
I made this script long ago so it isn’t really efficient but it gives a good idea how it’s done
server
local RS = game:GetService("ReplicatedStorage")
local event = RS.InventoryEvent
local eventRemove = RS.InventoryEventRemove
game.Players.PlayerAdded:Connect(function(plr)
local inventory = Instance.new("Folder",plr) --creates inventory folder for our items
inventory.Name = "Inventory"
inventory.ChildAdded:Connect(function(item) --if an object is added to our inventory it fires an event to the client with the item's name.
event:FireClient(plr, item.Name, true)
end)
event.OnServerEvent:Connect(function(Player, itemname, Value) -- Event for dropping items from inventory into the 3d space
if Value == false then
local SI = Player.Inventory:FindFirstChild(itemname)
if SI:IsA("Model") then --if the object is a model we need to use another method
SI.Parent = game.Workspace
SI:SetPrimaryPartCFrame(Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2)) -- sets the dropped items position to the players rootpart with and offset of 2 studs in the z direction
else
SI.Parent = game.Workspace
SI.CFrame = Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2) --same as for model
end
end
end)
end)
client
local inventoryevent = game.ReplicatedStorage.InventoryEvent
local itemframe = script.Parent:FindFirstChild("ItemFrame") --The itemframe that holds the text of the object and the quantity
local list = {} --this list is going to hold the quantity of one item. I use logs
local Player = game.Players.LocalPlayer
inventoryevent.OnClientEvent:Connect(function(ItemName, Value) --fires when an item is added to the inventory
if Value == true then
local itemButton = itemframe:FindFirstChild("Item"):Clone() --clones the itembutton for the possible new item
itemButton.Visible = true
itemButton.Name = ItemName
if itemframe:FindFirstChild(ItemName) then -- if the item already exists we want to stack it
table.insert(list, ItemName) --inserting the itemname to the list
itemButton:Destroy() -- destroys since the item already exists
itemframe:FindFirstChild(ItemName).Text = ItemName.." "..#list+1 - changes name to the quantity of the item the player has
else --if it's a new item we won't destroy the itembutton
itemButton.Parent = itemframe
itemButton.Text = ItemName.." "..#list+1
end
itemButton.MouseButton1Click:Connect(function() --dropping item
if #list > 0 then -- if the ammount of "logs" is more than 0 then we remove 1 "log" from the list and updates the UI accordingly to the ammount of "logs"
table.remove(list, 1)
itemButton.Text = ItemName.." "..#list+1
else
itemButton:Destroy() -- if it's the last "log" remaining we destroy the UI since were out of "logs"
end
inventoryevent:FireServer(ItemName, false) -- fires to the server and removes the object and inserts it into the workspace.
end)
end
end)