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?
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!!
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.