I made a backpack script check it and find the error pls.The script is
while wait() do
if game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name) then
for i, v in pairs(game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name)) do
if v.Name == script.Parent.Parent.Name then
script.Parent.Text = i
end
end
end
end
So what it does is if player has a number of the same tool in their backpack. it should show the number how many are there in his backpack.But it says error pls tell how to fix it.
while wait() do
if game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name) then
for i, v in pairs(game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name)) do
if v.Name == script.Parent.Parent.Name then
script.Parent.Text = i
end
end
end
end
while wait() do
if game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name) then
script.Parent.Text = game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name)
end
end
The error is that GetFirstChild will only get the first instance with the script.parent.parents name. so you should use GetChildren which would get ALL of instances under the BackPack then see if its the same as the script.Parent.Parent.Name. the script below should work.
while wait() do
if game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name) then
for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren) do
if v.Name == script.Parent.Parent.Name then
script.Parent.Text = i
end
end
end
end
For the script of GetChildren() , what its doing is its checking if there is the tool and if yes its showing backpack all tools count, instead of counting the tool with same name.
Using the for loop, you are looping through the objects inside of a folder or storage, but by saying BackPack:FindFirstChild(), you are making an error because you are looping through one object, which won’t work.
Hold on is this script is supposed to count how many of this item is in the backpack right? if so you could do
function check(name)
local number = 0
local children = game.Players.LocalPlayer.Backpack:GetChildren()
for i,v in pairs(children) do
if v.Name == name then
number = number + 1
end
end
return number
end
while wait() do
if game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name) then
local name = script.Parent.Parent.Name
local amount = check(name)
script.Parent.Text = amount
end
end
As the script you gave is a looped function what its doing is its checking once and adding a number.But since its a looped function for i,v and while wait do functions.Its checking and correcting of number looped and the number is increasing from 1 to inf in a few seconds.Causing lag for the player.