How to loop in the order of the dictionaries

I want to be able to loop in the exact order the dictionaries are written or maybe give them some sort of a “order”, I am confused on how to be able to achieve such a thing.

My module:

local modes = {
	["Kaioken"] = {
		Name = "Kaioken";
		Boost = 1;
		Color = Color3.fromRGB(255, 0, 0);
		---
		Attack = 7000;
		Agility = 7000;
		Ki = 7000;
	};
	["SSJ"] = {
		Name = "Super Saiyan";
		Boost = 3;
		Color = Color3.fromRGB(255, 231, 111);
		---
		Attack = 25000;
		Agility = 25000;
		Ki = 25000;
	};
}
return modes

The looping:

-- Modes
for i, v in ipairs(ModesData) do
	local meleeName = i
	local button = button:Clone()

	button.Name = v.Name
	button.Text = v.Name
	button.Visible = true
	button.Parent = dojoframe:WaitForChild("ModesFrame")
	button.MouseButton1Click:Connect(function()
		selectingSkillGUI(v,"Modes","Selected","Select")
	end)
end

You would just have to store them in arrays instead. This wouldn’t be too bad since you already have a Name value.

And you could index it the same

local dict = {...} -- put your array

local function GetAttack(name)
  for i,v in pairs(dict) do
    if v.Name and v.Name == name then
     return v
    end
  end
end
2 Likes

The module:

local modes = {
	{
		Name = "Kaioken";
		Boost = 1;
		Color = Color3.fromRGB(255, 0, 0);
		---
		Attack = 7000;
		Agility = 7000;
		Ki = 7000;
	};
	{
		Name = "Super Saiyan";
		Boost = 3;
		Color = Color3.fromRGB(255, 231, 111);
		---
		Attack = 25000;
		Agility = 25000;
		Ki = 25000;
	};
}

return modes

The looping:

-- Modes
for _, data in ipairs(ModesData) do
	local button = button:Clone()

	button.Name = data.Name
	button.Text = data.Name
	button.Visible = true
	button.Parent = dojoframe:WaitForChild("ModesFrame")

	button.MouseButton1Click:Connect(function()
		selectingSkillGUI(data, "Modes", "Selected", "Select")
	end)
end

you already have the name inside the table, so you can just do it like this

1 Like
local modes = {
	{["Kaioken"] = {
		Name = "Kaioken";
		Boost = 1;
		Color = Color3.fromRGB(255, 0, 0);
		---
		Attack = 7000;
		Agility = 7000;
		Ki = 7000;
	}},
	{["SSJ"] = {
		Name = "Super Saiyan";
		Boost = 3;
		Color = Color3.fromRGB(255, 231, 111);
		---
		Attack = 25000;
		Agility = 25000;
		Ki = 25000;
	}}
}

You could also do this to convert the dictionary into an array and then perform the ipairs() iterator on it.

2 Likes