Script does not recognise items in the Backpack

Hello!

In my script, I have made a table which has items and the value for them. (E.g, Nespaper is worth $3)

After the table I have made some code which checks if any of the items in the table are in the player’s backpack and if so print out a statement.
However the script runs once and even when I have a Newspaper as a tool in my starterpack to begin with. It says,

Item not found in the Items table.

Any solutions?

local Items = {
    ["Newspaper"] = 3,
    ["Rat"] = 2,
    ["Broken furniture"] = 70,
    ["Toy"] = 8,
    ["Bottle"] = 3,
    ["Shoes"] = 13,
    ["Monitor"] = 85,
    ["Clothes"] = 10,
    ["Scrap metal"] = 2,
    ["Cardboard"] = 4,
    ["Book"] = 4,
    ["Plastic container"] = 7,
    ["Food scraps"] = 13,
    ["Hammer"] = 30,
    ["Can"] = 3,
    ["Magazines"] = 12,
    ["Dishes"] = 27,
    ["Batteries"] = 25,
    ["Desk"] = 115,
    ["Chair"] = 43,
    ["Keyboard"] = 58,
    ["Lights"] = 43,
    ["PC"] = 468,
    ["Fridge"] = 324,
    ["Microwave"] = 286,
    ["Paint Brush"] = 11,
    ["Paint Can"] = 7
}

local plr = game.Players.LocalPlayer
local tool = plr.Backpack:FindFirstChildWhichIsA("Tool")

if not tool then
    print("No tool found in the backpack.")
else
    print("tool")
    local ItemName = tool.Name
	local ItemValue = Items[ItemName]
	print(ItemValue)
    if ItemValue then
        print(ItemName .. " is worth " .. ItemValue)
    else
        print("Item not found in the Items table.")
    end
end

what does ItemValue print out?

The ItemValue doesn’t print for some reason.

try print tool then

limit

limit again

also this is a local script right? (just checking)

Nope when I do Print(tool) it doesn’t print either.

yes its a local script

probably because the tool isnt loading in when the script runs

try doing

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()

Or

repeat task.wait() until player.Character:FindFirstChildWhichIsA("Tool") 

yeah that was what i was abt to say

I tried the first example but nothing printed out except for:
“Item not found in the Items table.”
which prints when the player joins the game.

try the second, it gurantees that a tool exists in the backpack when the next code runs

Would I need to replace the exsisting:

local tool = plr.Backpack:FindFirstChildWhichIsA(“Tool”)

Because then it removes the variable

no, just chuck it before that line

1 Like

When I pull out the tool (the item name is Food Scraps) it then only prints out

“Item not found in the Items table.”

And it only prints once, so when I have more tools in my backpack and pull it out, it doesn’t print out anything.

Heya! When a player equips a tool, it moves from the backpack to the character.

Your script runs once because it’s not tied to any events / loops that will invoke it to run again. It also may run before any tools loaded into the backpack, which is why you’re getting that message.

If you’re trying to print the value of an item once equipped. You can check when a tool is inserted into the player’s character. E.g,

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

character.ChildAdded:Connect(function(newChild)
       if not newChild:IsA("Tool") then return end

       local value = Items[newChild.Name]
       if value then
         print(newChild.Name .. " is worth: " .. value)
         else
         print(newChild.Name .. " not found in table")
       end
end)

I’m on mobile so if the code is a mess sorry abt that, also if this script isn’t in StarterCharacterScripts then I recommend updating the character variable after the player respawns.

1 Like

Thank You! The tools value is printed when you hold out the tool!