Why is value nil when using table.find?

Hello!

In my script I’ve got a table with a bunch of contents in it.
Then I have got when the player equips a tool it connects a function and makes a variable where it uses table.find(table, Item)

I have also made a breakpoint.

Any solutions? Thanks in advance :slight_smile:

Script:

local Items = {
	{Name = "Newspaper", Cost = 3, Rarity = "Common"},
	{Name = "Rat", Cost  =  2, Rarity = "Common"},
	{Name = "Broken Furniture", Cost  = 70, Rarity = "Legendary"},
	{Name = "Toy", Cost  = 8, Rarity = "Uncommon"},
	{Name = "Bottle", Cost   = 3, Rarity = "Common"},
	{Name = "Shoes", Cost   = 13, Rarity = "Rare"},
	{Name = "Monitor", Cost  = 85, Rarity = "Legendary"},
	{Name = "Scrap Metal", Cost   = 2, Rarity = "Common"},
	{Name = "Clothes", Cost = 10, Rarity = "Uncommon"},
	{Name = "Cardboard", Cost  = 4, Rarity = "Common"},
	{Name = "Book", Cost  = 4, Rarity = "Common"},
	{Name = "Plastic Container", Cost  = 7, Rarity = "Uncommon"},
	{Name = "Food Scraps", Cost  = 13, Rarity = "Uncommon"},
	{Name = "Hammer", Cost  = 30, Rarity = "Rare"},
	{Name = "Can", Cost  = 3, Rarity = "Common"},
	{Name = "Magazines", Cost  = 12, Rarity = "Uncommon"},
	{Name = "Dishes", Cost  = 27, Rarity = "Rare"},
	{Name = "Batteries", Cost  = 25, Rarity = "Rare"},
	{Name = "Desk", Cost  = 115, Rarity = "Legendary"},
	{Name = "Chair", Cost  = 43, Rarity = "Rare"},
	{Name = "Keyboard", Cost  = 58, Rarity = "Legendary"},
	{Name = "Light", Cost  = 43, Rarity = "Legendary"},
	{Name = "PC", Cost  = 468, Rarity = "Mythic"},
	{Name = "Fridge", Cost  = 324, Rarity = "Mythic"},
	{Name = "Microwave", Cost  = 286, Rarity = "Mythic"},
	{Name = "Paint Brush", Cost   = 11, Rarity = "Uncommon"},
	{Name = "Paint Can", Cost  = 7, Rarity = "Uncommon"},
}

local player = game.Players.LocalPlayer
local RepStorage = game:GetService("ReplicatedStorage")
local character = player.Character or player.CharacterAdded:Wait()
local Backpack = player.Backpack

local Price = 0
local TotalPrice = 0 


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

	local value = table.find(Items, newChild.Name) -- newChild.Name is "Keyboard"
	if value then -- while value is nil and not "Keyboard"
		print(newChild.Name .. " is worth: " .. value)
		TotalPrice = TotalPrice + value
		Price = value
		print(TotalPrice)
	else
		print(newChild.Name .. " not found in table")
	end
end)

table.find only searches for values in an array style table (a table with indexed elements), but your table contains dictionary style entries with Name, Cost, and Rarity as keys. You need to manually loop through Items and check if newChild.Name matches the Name field.

Example below:

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

	local value = nil
	for _, item in ipairs(Items) do
		if item.Name == newChild.Name then
			value = item.Cost
			break
		end
	end

	if value then
		print(newChild.Name .. " is worth: " .. value)
		TotalPrice = TotalPrice + value
		Price = value
		print("Total Price: " .. TotalPrice)
	else
		print(newChild.Name .. " not found in table")
	end
end)
2 Likes

Thank you!

That is what I needed!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.