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()
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
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.
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.
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.
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