Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

A better way to force dictionaries to go in order?

Is there a better way I could make my dictionary go in order without having to refer to another table?

player_commands = {
	
	settings = {
		button_size = UDim2.new(0, 95,0, 30);
		max_col = 4;
		dir = options.players_commands;
		padding = 2;
		order = {
			
			'Kick';
			'Kill';
			'Explode';
			'Burn';
			'Load';
			'Tp_to_me';
			'Tp_to';
		}
	};
	
	commands = {
		
		Kick = function()
		
		end;
			
		Kill = function()
			
		end;
		
		Explode = function()
			
		end;
		
		Burn = function()
			
		end;
		
		Load = function()
			
		end;
		
		Tp_to_me = function()
			
		end;
		
		Tp_to = function()
			
		end;
		
	}
	
	
	
	
}

do
	local col 		= 0 
	local row 		= 0
	local count 	= 0 
	
	for a,b in pairs(player_commands.settings.order) do
		local v = player_commands.commands[b]
		-- Have to use this to go in order
		local button = create("TextButton")
		{
			Text = b;
			Name = b;
			BackgroundTransparency = (count % 2 == 0 and .3 or .5);
			BorderSizePixel = 0;
			Size = player_commands.settings.button_size;
			Font = "Legacy";
			FontSize = "Size12";
			TextColor3 = Color3.new(1,1,1);
			Position = UDim2.new(
				0,5 + ( (player_commands.settings.button_size.X.Offset * col ) + (col * player_commands.settings.padding)) , 
				0,5 + ( (player_commands.settings.button_size.Y.Offset * row ) + (row *  player_commands.settings.padding))); 
			Parent = player_commands.settings.dir
			
		}
		row = (col + 1 == player_commands.settings.max_col and row + 1 or row)
		col = (col + 1 == player_commands.settings.max_col and 0 or col + 1)
		count = count + 1
	end 
end

3 comments

0

The reason it's not going in a specific order is because the commands table does not have integer keys. You could modify the script so that each key/value pair is in its own table within the commands table. That way each command has an integer key, which allows it to go in the correct order.

0

I know, that's why ipairs is useless in this case. I was just wondering if there was something else I didn't know about.

0

Well, Lua stores non-integer keys in a hash table, so I doubt it's possible to preserve the order.

1 answer

✓ Accepted answer

As 2eggnog said, sadly it's not possible. But you can do something like:

commands = {
{"Kick"; function() end};
{"Kill"; function() end};

And so on to preserve order, but you will not be able to check if a command exists directly by key.

This discussion is preserved as it was. New answers, comments, and votes are closed.