Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to check if player has two of the same tool in Backpack?

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.

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")

2 comments

0

I can't get it to work still, what should I fix? https://gyazo.com/ca9a583d8765a9cb0cfd8abb39bb36f1

0

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.