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

Printing a table gives a random Value, why?

So, I've got a table where each key has two values. One Value is a number, and the other is a player name. Basically trying to make a high score table. However, when I print a key of the table to the output, I get this table: 32A0F2B8 instead of the Number & Name values.

Here's the code.

if GameEnd == true then 
	local PlrScore = player.leaderstats.Score.Value
	local Name = player.Name
	table.insert(PlayerScores, {PlrScore, Name})
end

table.sort(PlayerScores)
print(PlayerScores[1]) 

I am assuming its giving me this random number because of the dual-value key, but I could be wrong. I would like to know why though, whatever reason it is. Any help is appreciated, and if any clarification is needed please let me know. Thanks! :)

2 answers

✓ Accepted answer

The first index in the PlayerScores array is a table, so it prints the table's id. If you'd like to print everything in the table, loop through it:

for i,v in pairs(PlayerScores[1]) do
	print(v)
end

1 comment

0

Oh, thanks! Appreciate the help! :D

Memory

All computers have to store memory some how, and this is done by assigning a certain value, to a certain key. Typically when computing, we use hexadecimal (or, hex code) to store our references to certain values. The alphanumeric code you're getting when printing this table value, is it's memory address

Solution?

If you'd like a method to print everything inside your table, I've created an extension to the built-in table library specifically for ROBLOX Lua, you can take here: https://www.roblox.com/Extened-table-library-v4-item?id=386010252

Using this library extension, you can use the table.list or table.map function to view the content inside your table. Here's an example:

local table = require(386010252) -- My table library extension

local test = {
	x = {
		a = 1,
		b = 2,
		c = 3,
	},
	y = {
		a = 1,
		b = 2,
		c = 3,
	}
}

print(table.map(test)) -- shows everything in your table, including nested tables

print(table.list(test)) -- shows the initial values of the content in your table

Hope this helped, let me know if you have any questions.

2 comments

0

table.map is not a good name for a function that makes a string. "map" over a list means applying a function to each element of a list, and you'd expect it to work like this: table.map(test, print).

0

I see what you mean, though I was leaning towards the more traditional use of the term "map", meaning to just display a network of different routes and locations in a central area.

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