Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
ZiIIex 0
4 years ago

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

Log in to vote
0
Answered by 4 years ago

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")
0
I can't get it to work still, what should I fix? https://gyazo.com/ca9a583d8765a9cb0cfd8abb39bb36f1 ZiIIex 0 — 4y
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. LukeSmasher 619 — 4y
Ad

Answer this question