Is it possible to do this?

So I’ve created a little system that displays the stats of an item when you hover of it it’s probably not the best, but like whatever it works although I recently tried to make it like maybe a bit more efficent and was wondering if something like this was possible.

So I have an item that looks like this or I mean all my items look like this, but this is just one of them

	["Rusty Helmet"] = {
		Cost = 35,
		name = "Rusty Helmet",
		rarityText = "Common",
		rarityTextColor = Common,
		equipmentSlot = "Head",
		Tier = 1,
		Strength = 4,
		Defense = 6,
		Agility = 3,
	},

And inside my function for displaying the stats in my gui I just have

	itemInfoFrame.Power.Text = "Strength: "..items[item].Strength
	itemInfoFrame.Stamina.Text = "Defense: "..items[item].Defense
	itemInfoFrame.Agility.Text = "Agility: "..items[item].Agility

Although the issue here is not all the items have these same stats so it’s gonna error and so I decided what if I did something like this

	for i,v in pairs(items[item]) do
		if i == "Strength" or i == "Agility" or i == "Defense" then
			itemInfoFrame.Stats.Text = i..":".." "..v.."\n"..i..":".." "..v.."\n"..i..":".." "..v.."\n"
		end
	end

And ok so obviously this doesn’t work, but is it possible to do this and if so how? I feel like it could be better I wouldn’t have to use seperate text labels for each stat and like yeah lol.

There’s a much simpler way to do this.

Change you table to be set up like this:

["Rusty Helmet"] = {
		Cost = 35,
		name = "Rusty Helmet",
		rarityText = "Common",
		rarityTextColor = Common,
		equipmentSlot = "Head",
		Tier = 1,
        Stats = {
		    Strength = 4,
		    Defense = 6,
	    	Agility = 3,
        }
	}

Once you do that, you can just have the script iterate through items[item].Stats and for each key, value pair, just clone a text label that has text like “key..":"..v” For positioning, you can use a UIListLayout

Hope this made sense. Let me know if not.

Oh alright I can try this out however I actually found out I could do this a few seconds ago lmao

	local emptyString = ""
	for i,v in pairs(items[item]) do
		if i == "Strength" or i == "Agility" or i == "Defense" then
			emptyString ..= i..":".." "..v.."\n"
		end
	end

and this also seems to be able to do what I was trying to achieve.

1 Like

On top of Chatowillwin’s way, there’s another way in which I enjoy using. Its a lot easier for me to understand and reference.

["Rusty Helmet"] = {
		["cost"] = 35,
		["name"] = "Rusty Helmet",
		["rarityText"] = "Common",
		["rarityTextColor"] = Common,
		["equipmentSlot"] = "Head",
		["tier"] = 1,
        ["stats"] = {
		    ["Strength"] = 4,
		    ["Defense"] = 6,
	    	["Agility"] = 3,
        }
}

To reference any of these values, you’d do:

	itemInfoFrame.Power.Text = "Strength: "..items[item]["Stats"]["Strength"]
	itemInfoFrame.Stamina.Text = "Defense: "..items[item]["Stats"]["Defense"]
	itemInfoFrame.Agility.Text = "Agility: "..items[item]["Stats"]["Agility"]

If you want to check if an item has a stat, you could just do:

if items[item]["Stats"]["Strength"] then
 -- code here
end

If it doesn’t have that stat, it’ll just return nil. If it does have that stat, it’ll return the strength #

Wouldn’t I end up having to do this? And end up having a lot of ifs lol.

if items[item]["Stats"]["Strength"] then
	itemInfoFrame.Power.Text = "Strength: "..items[item]["Stats"]["Strength"]
end
if items[item]["Stats"]["Agility"] then
	itemInfoFrame.Agility.Text = "Agility: "..items[item]["Stats"]["Agility"]
end
if items[item]["Stats"]["Stamina"] then
	itemInfoFrame.Stamina.Text = "Stamina: "..items[item]["Stats"]["Stamina"]
end
--etc for each stat