How could I improve this function?

Hello,

I am currently making a function that will turn an array into a viewable string. Here’s an example: {"hi", 1, 2, 3, "other", true} will turn into "{[1] = "hi", [2] = 1, [3] = 2, [4] = 3, [5] = "other", [6] = true}"

Here is the code that I need reviewed, it seems very long and I know there’s some way to improve it at least.

function Package.Utility.arrayToString<a>(t: {[number]: a}, sep: string?, i: number?, j: number?): string?
	local stringToConvert = "{"
	
	sep = Package.Utility.nilparam(sep, ", ")
	i = Package.Utility.nilparam(i, 1)
	j = Package.Utility.nilparam(j, #t)
	
	if i <= 0 or i > #t then
		Debugger.silenterror(`Field 'i' must be greater than 0 and less than or equal to {#t}.`)
		return
	end
	
	if j <= 0 or j > #t then
		Debugger.silenterror(`Field 'j' must be greater than 0 and less than or equal to {#t}.`)
		return
	end
	
	for index = i :: number, j :: number do
		local value = t[index]
		if type(value) == "table" and not Package.Utility.isdictionary(value) then
			value = Package.Utility.arrayToString(value)
		end
		if type(value) == "string" then
			value = `"{value}"`
		end
		if index == j then
			stringToConvert = `{stringToConvert}[{index}] = {value}}`
		else
			stringToConvert = `{stringToConvert}[{index}] = {value}{sep}`
		end
	end
	
	return stringToConvert
end

That seems a little complicated, so I decided to make an example of how I’d do it so maybe it could help you to simple it down:

local Array = {"hi", 1, 2, 3, "other", true, workspace.Baseplate,{1,"Test2"},"Test"} 

function TableToString(TheArray) 
	local Outline = "{" 
	for i,v in pairs(TheArray) do 
		if typeof(v) == "string" then 
			Outline = Outline.."["..i.."] = ".."\""..v.."\"" 
		elseif typeof(v) == "Instance" then 
			local Path = v:GetFullName() 
			local appendStr = "game" 

			-- Assuming you're sane and don't put periods in instance names 
			for i,v in ipairs(Path:split(".")) do 
				local appendToken = v 

				if v:match(" ") then 
					appendToken = "[\"".. v.."\"]" 
				else 
					appendToken = "."..v 
				end 
				
				appendStr = appendStr..appendToken 
			end 
			Outline = Outline.."["..i.."] = "..appendStr 
		elseif typeof(v) == "table" then 
			Outline = Outline.."["..i.."] = "..TableToString(v) 
		else 
			Outline = Outline.."["..i.."] = "..tostring(v) 
		end 
		if i ~= #TheArray then 
			Outline = Outline..", " 
		end 
	end 
	Outline = Outline.."}" 
	return Outline 
end 

print(TableToString(Array)) 

I don’t know if any of this helps, but I gave some pretty simple methods and examples at least so maybe it’ll be useful?

for the Instance part I found something online and used that so I decided to put the same comment there about it not working properly if there is a dot in the name of it lol but that’s the only one with an exception as far as I know of

Ah, you’re right! I should use Instance:GetFullName() thanks!