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