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

How can I check if a player has X amount of something in their backpack?

Asked by 8 years ago

Say I make a crafting system in my game. You needs 5 apples to craft 1 golden apple. The apples are tools in the player's backpack. How can I go about checking if the player has 5 individual apples in their backpack without ending up checking the same apple 5 times?

2 answers

Log in to vote
1
Answered by 8 years ago

Why not use for loops to check the amount of apple in Player's Backpack?

local Player = game.Players.LocalPlayer
local Apple = 0 

for _,v in pairs(Player:WaitForChild("Backpack"):GetChildren()) do--Get the Children's in Player's Backpack.
    if v.Name == "Apple" then--Check if Backpack's Child name is Apple if so continue.
    Apple.Value = Apple.Value + 1--Keeps track the number of apple.
    end
end

~UserOnly20Charcters, Hoped I helped you to answer your question! If you have any further question, don't hesitate to comment below!!

Ad
Log in to vote
0
Answered by
azabat 10
8 years ago
count = 0
local children = player.Backpack:GetChildren()
while true do
    for i = 1, #children do
        if children[i].Name = "Apple" then
        count = count + 1
    end
    if count >= 5 then
        -- you can make an apple
    end
wait(1)
end

So basically when you make the golden apple, you should reduce the count value by 5.

Answer this question