What is wrong with this code?

So, this code prints tool’s name that you have in inventory and has value of true in table.

local Player = game.Players.LocalPlayer
local Table = {
    ["Keycard1"] = true
}

for _, tool in ipairs(Player.Backpack) do
    if Table[tool.Name] == true then
        print(tool.Name) 
    end
end

for _, tool in ipairs(Player.Character) do
    if tool:IsA("Tool") and Table[tool.Name] == true then
        print(tool.Name) 
    end
end

After I run this, I get “Backpack is not a valid member of Player “Players.18890miki” error. Can someone help me?

ipairs and pairs is used for looping through tables, not an instance (you can but don’t),
so it’d be Player.Character:GetChildren() and Player.Backpack:GetChildren()

1 Like
local Player = game.Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Table = {
    ["Keycard1"] = true
}

for _, tool in pairs(Backpack:GetChildren()) do
    if Table[tool.Name] == true then
        print(tool.Name) 
    end
end

for _, tool in pairs(Character:GetChildren()) do
    if tool:IsA("Tool") and Table[tool.Name] == true then
        print(tool.Name) 
    end
end
1 Like

Ah yes I didn’t notice that let me fix my solution.

Firstly, declare a new local variable so we can be sure backpack has loaded in

local Backpack = Player:WaitForChild("Backpack")

Secondly, ipairs should not be used for this, you want to use pairs instead

Thirdly, you aren’t getting the children from Backpack or Character, you must do Backpack:GetChildren() otherwise there is no table to iterate though

1 Like

It is not printing anything even that I have it in my inventory

@Viperize @shadowmaster940

May you help me too?

It could be because the things in your inventory get added to your inventory after the code has ran. So while the code was running you didn’t have anything in your inventory.

1 Like

What can I do so it wont happen?

Maybe replace the loops with something like this

Backpack.ChildAdded:Connect(function(child)
	if child:IsA("Tool") and Table[child.Name] then
		print(child.Name)
	end
end)

This way it will listen to every tool added, even after the code has finished running. (until you disconnect the event)

1 Like

Still doesnt print anything

ddsfwdfdserfwt

Ok how about this:

function checkChild(child)
	if child:IsA("Tool") and Table[child.Name] then
		print(child.Name)
	end
end

Backpack.ChildAdded:Connect(checkChild)
Character.ChildAdded:Connect(checkChild)

Now it also prints for equipped tools. But now you have to add some code to disconnect the character event when the character is removed, and some code to reconnect it when a new character is added.

1 Like

Try setting a breakpoint (click to place red dot next to the line number), start the game, and look at the Watch window when execution stops at the breakpoint. You can set the breakpoint right at the start and step over the statements one by one to see where it goes wrong.

1 Like

I checked, it only prints equipeed tools not that are in backpack to. About that other code I should be able to get it.

How does that work? I never used debugging before

local Player = game.Players.LocalPlayer
local Table = {
    ["Keycard1"] = true
}
repeat task.wait() until Player.Backpack ~= nil and Player.Character ~= nil
suc,err = pcall(function()
if not Player:FindFirstChild("Backpack") then
error("Could not find backpack")
end
if Player.Character == nil then
error("Character is nil")
end
end)
print(err)
for _, tool in pairs(Player.Backpack:GetChildren()) do
    if Table[tool.Name] == true then
        print(tool.Name) 
    end
end

for _, tool in pairs(Player.Character:GetChildren()) do
    if tool:IsA("Tool") and Table[tool.Name] == true then
        print(tool.Name) 
    end
end

maybe try this though im not sure why the backpack detects as not loaded
if any error pops up please tell me

1 Like

It prints tool’s name on spawn, but also prints “nil”

1 Like

the nil means there is no error in pcall which means it worked

1 Like

Oh, ok. Thank you so much

dsdsad

1 Like

no problem im glad to help you can remove the printing

which prints as nil

1 Like