Basic Table Functions

Hi! Working in Roblox for any given amount of projects of any level of difficulty, dealing with Tables is unavoidable.

I found that I had to keep writing the same functions across multiple projects when it came to handling Tables, whether it be Dictionaries and Arrays. Here is this module called “TableFunctions” that is just some functions I either use or think would be useful to others.

I will regularly add more functions over time depending on what I think might be useful to others or myself.

I’m not claiming for this to be unique or better than anyone that currently might have a similar module, this is just a resource to use if you would like!

Check it out on github if you are interested.

3 Likes

Just a suggestion; you can accept any value in array without losing types via generics. For example:

function TableFunctions.InvertArray<T>(array: {T}): {T}
	local newArray = {}
	for _, value in array do
		table.insert(newArray, 1, value)
	end
	return newArray
end
TableFunctions.InvertArray({1, 2, 3, 4}) --> Return Type: {number}
TableFunctions.InvertArray({false, true, false}) --> Return Type: {boolean}
TableFunctions.InvertArray({"a", "b", "c", "d"}) --> Return Type: {string}
5 Likes

Never utilized generics before, definitely gonna use this instead!

3 Likes
function TableFunctions.InvertArray(Array: {[number]: any}) : {[number]: any}
	local InvertedArray: {[number]: any} = {}
	local Index = 1
	for Count = #Array, 1, -1 do
		InvertedArray[Index] = Array[Count]
		Index += Index
	end
	
	return InvertedArray
end

I’d like to let you know that table.sort() already achieves this

1 Like

I know you can use table.sort() to achieve the same but part of what I hope the attraction of the modules is to have a quick and easy function to use instead of having to write a comp function for table.sort(). Some of these functions aren’t included because they are not default given by roblox, but that they might be a quicker and easy alternative that can save you time.