Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How do you get info from a table?

Asked by
Dr_Doge 100
8 years ago

So I have this code:

function GetData() -- returns the "data" table
info = game:service'HttpService':GetAsync('http://pastebin.com/raw/zvzSTdXh');
info = game:service'HttpService':JSONDecode (info);
return info.data
end

How would I get each name from that table?

Please help. This is one of my first times working with tables for a project, and its confusing.

1 answer

Log in to vote
1
Answered by 8 years ago

Overview

A table is a data structure used to store data by associating it with keys you can reference to access said data (just like ordinary variables). However unlike ordinary variables, a table gives you the ability to group these sets of variables to keep private, or to iterate with a for loop. It's also necessary to keep your code organized and clean, if used properly.

Getting data from a table all at once

As you probably know (or at least, should know), you can index a table for a specific value by referencing it's corrosponding key (aka, variable). However in some cases, you may want to work with all values in the table no matter what they are. For this, we can simply use a for loop.

For loops?

There are two types of for loops; numerc and generic. Numeric for loops will simply execute it's code a set amount of times, which you can control with it's defult variant (the variable you declare before you set the number range).

A generic for loop is basically anything that's not a numeric for loop (thus the term, generic). You can use a generic for loop to iterate dictionary style tables (tables with exclusively defined keys - not an array)

I'll leave the vauge terms for you to find out, cause I don't wanna lean away from the topic. If you wanted to get every value from a table, you should practice using generic for loops. Here's an example:

-- Dictionary
local tab = {x = "a", y = "b", z = "c"}

-- Generic for
for i,v in pairs(tab) do
    print(i,v) -- inside this loop, we can use "i" to represent the keys (variables) and "v" to represent it's corrosponding value (obviously, you can call these variables whatever you want)
end

Hope that helped. If you have any questions, let me know.

Ad

Answer this question