Can someone break down the details of what these 2 functions do?

So I was watching this datastore tutorial and around the 6:05 timestamp of the video, he just pastes these two functions (PrintTableFunction and PrintTable)out of no where without any in-depth explanation claiming that it’s to help test out the datastore code. But I really want to understand how it works,(excerpt):

local function PrintTableFunction(Table,string)
	for i,v in pairs(Table) do
		if type(v) == "table" then
			print(string,i," : ",v,":::")
			PrintTableFunction(v, string.."     ")
		else
			print(string,i," : ",v)
		end
	end
end

local function PrintTable(Table)
	print()print()PrintTableFunction(Table, "")print()print()
end

Players.PlayerAdded:Connect(function(Player)
	LoadData(Player)
	ServerData[Player].Coins = ServerData[Player].Coins + 5
	PrintTable(ServerData[Player])
end)


Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player)
end)

The code within these 2 functions make little sense to me, why equate type(v) to the string “table”, what does type() even do? What’s it meant to do? And I don’t get the ensuing code with this odd print statement print(string,i," : ",v,":::") . Not to mention, I didn’t know that calling a function within itself was possible, the v,string…""?
Well I could go on further but then it would be a lot more waffle than there already is, in short, it’s all very confusing to me. Would be great if someone could break both of those functions down for me and explain how exactly they test out the datastore.

1 Like

I’m pretty sure it’s just printing the table out? I could be wrong.

2 Likes

All it does is print tables in a specific way that is a bit more readable than the straightforward manner.

Try it! But in short, it evaluates to the type of whatever parameter you pass to it, expressed as a string. So type("test") evaluates to “string”, type({a = 1}) evaluates to “table” and type(math.floor) evaluates to function.

So that the code can do one thing if v is a table, and another thing if it’s not a table.

Passing multiple parameters to print just makes it print each thing in order, just like having several print statements. So it’s exactly like

print(string)
print(i)
print(" : ")
print(v)
print(":::")

It’s totally possible, and a super powerful programming technique! Look up recursion if you want to know more. In this case it’s used to print tables because tables are potentially recursive data structures, because tables can contain other tables. So having a function that calls itself turns out to be about the simplest way of visiting every value within a table, considering that you might have to visit every value in each sub table, sub sub table, etc.

Here’s the whole loc: PrintTableFunction(v, string.." "). Notice that v is definitely a table that is inside whatever Table the function was called on previously, which is ensured by the if type(v) == "table" then condition.

So all in all, the function prints every value in a table, and if any of those values are tables themselves, then it prints those values as tables (meaning all their values and sub tables etc etc). Neither function is actually important or has anything to do with datastores.

Anyway none of it matters because a recent-ish update made the Output way better, so now it pretty-prints tables without developers having to use functions like these. Just turn off log mode and turn on expand tables:

2 Likes

But wont equating type(v) to a string(“table”) check whether v is a string since this “table” is just a string and not an actual table?

I see, btw I don’t understand what the keyword string does in general and in this scenario, I tried printing it but it just outputs a weird list of functions, I looked it up and I don’t understand the explanation on the devhub.

well that’s a relief.

It’s not a keyword, it’s just a “built-in” variable that refers to the string library. It can be overwritten like in this case, but it’s generally a bad idea because it’s confusing.

Try running this script to understand how it works:

print(
typeof("test"),
typeof({}),
typeof({a = 1}),
typeof({1, 2, 3}),
typeof(1),
typeof(true),
typeof(print),
typeof(typeof),
typeof(typeof(1)),
typeof(string),
typeof(math),
typeof({}) == "table",
typeof("test") == "table",
)
1 Like

so this “string” is basically just a table? I don’t get what is the point of printing it on its own.

I assume in an earlier portion of the script, string was overwritten by a different value. By default, the string global is indeed a table.

Running print(string) in a newly created script or in the command bar, will show you it’s functions. Or you can look here

1 Like