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

Print out concatenated array?

Long story short, I have a leader board that shows the players job title and a script with the globals in which contain arrays of all the info about those jobs.

I'm trying to print the value of one of the jobs, but to find out which job the player has it gets the name from the leader board and which is the same name as the global var, then grabs the amount of money they earn from that array.

For example;

	Job = game.Players.LocalPlayer.leaderstats.Job.Value
	print('_G.' .. Job .. '[3]')

And here is the global in another script...

_G.WesteriesEmployee = {"WesteriesEmployee",2,40,"Westeries Employee"} -- Food+Drink Shop Cashier

So basically I need to to grab the name of the job from the leader board, realise that it's going to grab the info from the global var/array and then print the result instead of printing this...

_G.WesteriesEmployee[3]

Thanks in advance for any help!

1 answer

✓ Accepted answer

Indexing

I don't think you really learned how indexing a table works, because you're already doing it once for the array, but not for the dictionary.

Dictionaries are sweet, like syntax sugar.

When you do table.Name you're actually doing table["Name"]. The translation is done internally as part of Lua's wonderful syntax sugar which helps keep the language explicit but equally abstract. This makes sure that it's easy to read and write, but also makes sure that it's as fast as it can be whilst being as simple as it can be by not differentiating things internally.

Away with the tablecloth

Fixing your code is as simple as looking past the syntax abstraction and indexing the table explicitly

Job = game.Players.LocalPlayer.leaderstats.Job.Value
print(_G[Job][3])

Isn't that convenient?

1 comment

0

Thanks for the answer it gave me a bit of an insight into it. I worked it out exactly how you did it before reading your answer but had no idea why it worked that way. So thanks a lot!

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