Finding same value in a table

I’m new to tables so i dont know wat i am doing so wat i am trying to so is find the ItemID inside the table and get the info inside it but idk how

here is the script for my inventory i made :

local Player = game.Players.LocalPlayer

local itemDATA = require(game.ReplicatedStorage.Itemdata)
local InventoryHandler = require(script.Parent.ModuleScript)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")

local inventoryGUI = script.Parent
local Frame = inventoryGUI:WaitForChild("Items")
local Template = inventoryGUI.Template:WaitForChild("Template")

local DataFolder = ReplicatedStorage.ReplicatedData:WaitForChild(Player.UserId)

DataFolder.Inventory.Changed:Connect(function(encodedData)
	local dataTable = HttpService:JSONDecode(encodedData)
	for itemName, amount in pairs(dataTable) do
		local label = Frame:FindFirstChild(itemName)
		print(table.find(itemDATA.data))
		if (not label) then
			label = Template:Clone()
			label.Name = itemName
			label.Parent = Frame
			label.Visible = true
		end
	end
end)

here is the code i made :

print(table.find(itemDATA.data))

here is the itemData :

local Items = {}

Items.data = {
	{
		Name = "Hat";
		ImageID = 1409499909;
		Cost = 100;
		Value = 10;
		Tier = "Commen";
		ItemID = 1
	};
	{
		Name = "Pants";
		ImageID = 128302713;
		Cost = 30;
		Value = 100;
		Tier = "Commen";
		ItemID = 2
	};
	{
		Name = "Shirt";
		ImageID = 495262405;
		Cost = 330;
		Value = 1000;
		Tier = "Commen";
		ItemID = 3
	};
	{
		Name = "Glasses";
		ImageID = 1439396148;
		Cost = 2000;
		Value = 1000;
		Tier = "Uncommen";
		ItemID = 4
	};
	{	
		Name = "Chain";
		ImageID = 1027719587;
		Cost = 1233;
		Value = 4623;
		Tier = "Uncommen";
		ItemID = 5
	};
}

return Items

the data is saved in DataStore2 here is the saved data :

{"2":1,"4",:3}

so i need help pls

When i make modules to store information like this I usually have a function at the very bottom for finding the information via ID or Name

Heres of an Example: Of a function i would add at the end of your items module

Items.Get_Item_FromID(ID)
function Items.Get_Items_FromID(ID)
	for Items,Item in pairs(Items) do --// Looping thru module		
			if type(Item)== "table" and Item.ItemID == ID then --// Checking if Item is a table and if ID_Parameter == ID
				return Item --// Returns Item if found
			end 
		end
	end
end

I want to show you how you can loop through the data table. But, I don’t understand what your data table means…

Can you explain how you store your data?

I will explain the “2” is the stored ItemID as you can see in the itemdata its Pants and the : 1 is the amount I have

What I’m going to recommend would require your to call your items module (preferable outside of the function like your other variables)

for itemName, amount in pairs(dataTable) do
 local ItemInfo = Items.Get_Item_FromID(itemName) --// You can use my example if u like or make your own up to you
 print(teminfo)

--// Label creation here
end

If you have any questions feel free to ask.

it said attempt to call nil value at

local ItemInfo = itemDATA.Get_Item_FromID(itemName) --// You can use my example if u like or make your own up to you

Do you have the function inside the module?

yes i did and it did not work for some reason

No errors? can you send what you copied. Actually nvm.

function Items.Get_Items_FromID(ID)
	for Items,Item in pairs(Items) do --// Looping thru module		
		if type(Item)== "table" and Item.ItemID == ID then --// Checking if Item is a table and if ID_Parameter == ID
			return Item --// Returns Item if found
		end 
	end
end

There was an Extra end

i know i deleted it and that gave no errors

Have you added prints?

function Items.Get_Items_FromID(ID)
print("Checking For ItemID")
	for Items,Item in pairs(Items) do --// Looping thru module	
	print(Items,Item)
		if type(Item)== "table" and Item.ItemID == ID then --// Checking if Item is a table and if ID_Parameter == ID
    print(Item)
			return Item --// Returns Item if found
		end 
	end
end

Did you change in pairs(Items) to be the name of your module

	for Items,Item in pairs(Name of your module) do --// Looping thru module	
1 Like

the local script does not even fire the function

Does your Loop in the local script run?

the script stops at the place where it needs to fire

Can I see the edits you made to the function in the module

here is the whole module script and local script

local script :

local Player = game.Players.LocalPlayer

local itemDATA = require(game.ReplicatedStorage.Itemdata)
local InventoryHandler = require(script.Parent.ModuleScript)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")

local inventoryGUI = script.Parent
local Frame = inventoryGUI:WaitForChild("Items")
local Template = inventoryGUI.Template:WaitForChild("Template")

local DataFolder = ReplicatedStorage.ReplicatedData:WaitForChild(Player.UserId)

DataFolder.Inventory.Changed:Connect(function(encodedData)
	local dataTable = HttpService:JSONDecode(encodedData)
	for itemName, amount in pairs(dataTable) do
		local ItemInfo = itemDATA.Get_Item_FromID(itemName) --// You can use my example if u like or make your own up to you
		print(ItemInfo)

		--// Label creation here
	end
	--[[for itemName, amount in pairs(dataTable) do
		local label = Frame:FindFirstChild(itemName)
		print(table.find(itemDATA.data))
		if (not label) then
			label = Template:Clone()
			label.Name = itemName
			label.Parent = Frame
			label.Visible = true
		end
	end]]--
end)

module script :

local Items = {}

Items.data = {
	{
		Name = "Hat";
		ImageID = 1409499909;
		Cost = 100;
		Value = 10;
		Tier = "Commen";
		ItemID = 1
	};
	{
		Name = "Pants";
		ImageID = 128302713;
		Cost = 30;
		Value = 100;
		Tier = "Commen";
		ItemID = 2
	};
	{
		Name = "Shirt";
		ImageID = 495262405;
		Cost = 330;
		Value = 1000;
		Tier = "Commen";
		ItemID = 3
	};
	{
		Name = "Glasses";
		ImageID = 1439396148;
		Cost = 2000;
		Value = 1000;
		Tier = "Uncommen";
		ItemID = 4
	};
	{	
		Name = "Chain";
		ImageID = 1027719587;
		Cost = 1233;
		Value = 4623;
		Tier = "Uncommen";
		ItemID = 5
	};
}

function Items.Get_Items_FromID(ID)
	print("Checking For ItemID")
	for Items,Item in pairs(Items) do --// Looping thru module	
		print(Items,Item)
		if type(Item)== "table" and Item.ItemID == ID then --// Checking if Item is a table and if ID_Parameter == ID
			print(Item)
			return Item --// Returns Item if found
		end 
	end
end

return Items

Okay I see a possible problem

	for Items,Item in pairs(Items.data) do --// Looping thru table within module
1 Like

it worked but now when it prints

print(ItemInfo)

it says nil
the module printed tho

I’m trying to see what could cause that. can I see a screenshot or a copy and paste of your output?

yes and it for some reason it printed all the data not 1

output :

  17:16:53.613  Checking For ItemID  -  Client - Itemdata:47
  17:16:53.614  1  ▼  {
                    ["Cost"] = 100,
                    ["ImageID"] = 1409499909,
                    ["ItemID"] = 1,
                    ["Name"] = "Hat",
                    ["Tier"] = "Commen",
                    ["Value"] = 10
                 }  -  Client - Itemdata:49
  17:16:53.614  2  ▼  {
                    ["Cost"] = 30,
                    ["ImageID"] = 128302713,
                    ["ItemID"] = 2,
                    ["Name"] = "Pants",
                    ["Tier"] = "Commen",
                    ["Value"] = 100
                 }  -  Client - Itemdata:49
  17:16:53.614  3  ▼  {
                    ["Cost"] = 330,
                    ["ImageID"] = 495262405,
                    ["ItemID"] = 3,
                    ["Name"] = "Shirt",
                    ["Tier"] = "Commen",
                    ["Value"] = 1000
                 }  -  Client - Itemdata:49
  17:16:53.614  4  ▼  {
                    ["Cost"] = 2000,
                    ["ImageID"] = 1439396148,
                    ["ItemID"] = 4,
                    ["Name"] = "Glasses",
                    ["Tier"] = "Uncommen",
                    ["Value"] = 1000
                 }  -  Client - Itemdata:49
  17:16:53.614  5  ▼  {
                    ["Cost"] = 1233,
                    ["ImageID"] = 1027719587,
                    ["ItemID"] = 5,
                    ["Name"] = "Chain",
                    ["Tier"] = "Uncommen",
                    ["Value"] = 4623
                 }  -  Client - Itemdata:49
  17:16:53.614  nil  -  Client - Controll:19