Roblox Backpack Script Help

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.

First, can you format the code correctly?

2 Likes

What error is it showing?

Reformatted Code (In a code block)
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
2 Likes

Yes thats what I typed but if I paste the script the format is changing like that.

Capture

It shows this @xZylter

The error is in

 for i, v in pairs(game.Players.LocalPlayer.Backpack:FindFirstChild(script.Parent.Parent.Name)) do

You have to get all the children of the backpack, so you have to use :GetChildren() like this

 for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
2 Likes
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

This should work.

1 Like

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
2 Likes

Sorry, its not working for me. I think it needs a complicated script.

1 Like

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.

1 Like

Are there any errors in the console?

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.

1 Like

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
2 Likes

Yes this is what I did before then it did not work and it says table got instance.I did a thing like

local number = 0
local name = script.Parent.Parent.Name
if v.Name = name then
number = number + 1
else
a call back function.

Thank you for it but it did not work saying got instance error

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.

I think it needs a pcall function to.If you think yes tell me what to do as a pcall pls.

Ok it works now. I changed the numbers to default as 1 in player inventory and to avoid the mistaken i fixed it.Thank you everyone who helped :slight_smile: