Weird table dilemma

local Icons = {
	["FriendlyHQ1"] = { Image = "http://www.roblox.com/asset/?id=8349723011" },
	["FriendlyHQ2"] = { Image = "http://www.roblox.com/asset/?id=8349723011" },
	["FriendlyHQ3"] = { Image = "http://www.roblox.com/asset/?id=8349723011" },
	["FriendlyHQ4"] = { Image = "http://www.roblox.com/asset/?id=8349723011" },
    variables would go on forever with the number after "FriendlyHQ"
}	

So this may be a simple question but how would I change these into one variable, instead of having a ridiculous amount of variables.

I also think a for loop would not work in this circumstance because it is a table and I just need it to be a variable.

Thanks! :slight_smile:

1 Like

You can’t put multiple values into a single variable, as each variable can hold one, and only one, value.

What are you trying to do with this??

So I have a map with icons that a player can place into and the script creates an image label for the icon. The script gets confused if there’s more than one icon with the same name so that’s what I am trying to accomplish with the variables.

Instead of having the key be the name, you can just use numbers and do something like:

[1] = { Name = "FriendlyHQ1", Image = "http://www.roblox.com/asset/?id=8349723011" },

that way even if they have the same name they wont replace each other

And use table.insert() for each one, then a for loop can identify said image and do the code you want to do, or you can also use table.sort

Let me offer a solution:

local Icons = {}
local Proto = {}

function Proto:Add(ContentString, ID)
  if ID then
    self[ID] = ContentString
    return ID
  end
  table.insert(self, ContentString)
  return table.find(self, ContentString)
end

setmetatable(Icons, {
  __index = Proto,
  __call = function(self, Term)
    assert(type(Term) == "number", "Invalid term for icon table!")
    if Term <= table.getn(self) then
      return self[Term]
    end
  end
})

-- Usage:

local FriendlyHQ1 = Icons:Add("http://www.roblox.com/asset/?id=8349723011")

local Result = Icons(1)

-- Test:
print(Result)

Edit: And if you want me to, I could make you a simple function to parse a jumble of these assets from a string and insert them consecutively into the table (Ideally this would result in you just pasting a large string full of icon urls, and calling the function with it as a parameter.)