I'm making a crafting system and I can't figure out how to see if a player has two of the same tool in their backpack.
COMMUNITY QUESTION
How to check if player has two of the same tool in Backpack?
1 answer
Iterating through the backpack and keeping tally should do fine for this.
local backpack = game.Players.LocalPlayer.Backpack
local function getToolCount(toolName)
local toolCount = 0
for i, child in pairs(backpack:GetChildren()) do
if child:IsA("Tool") and child.Name == toolName then
toolCount = toolCount + 1
end
end
return toolCount
end
-- eg: local swordsInBackack = getToolCount("Sword")
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
I can't get it to work still, what should I fix? https://gyazo.com/ca9a583d8765a9cb0cfd8abb39bb36f1
Don't call the function from within itself. Get the number of sticks in the backpack after the function, and do whatever you need to do with that number following that.