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

Can i find values of variable in a table using a string?

Asked by
BashGuy10 384 Moderation Voter
5 years ago
Edited 5 years ago

Hello, random people on the internet. I've been trying to find a way to get a value of a variable using a string, how can i search thru a table. Find a variable named something, and take that value? Anything to point me in the right direction?

My modulescript:

01local module = {}
02 
03local toolvalues = {
04    ["Stick"] = 1;
05    ["Bottle"] = 5;
06}
07 
08local repstorage = game.ReplicatedStorage
09 
10function module.Activate(tool) -- "tool" is the string of the name
11    repstorage.Remotes.Gain:FireServer(--[[What can i put in here to find the values, so how can i find the name of a variable in a table, then grab the value?]])
12 
13end
14 
15 
16 
17return module

2 answers

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

You use keys. Keys can be almost anything--userdata, strings, numbers, and even booleans. However, you probably shouldn't rely on userdata for a key.

Keys aren't specifically for tables however, they're like fancy dot notation (dot notation being Part.Mesh.Scale).

You can use keys in such a way to index properties of an Instance:

1local IN = Instance.new("Part");
2IN["BrickColor"] = BrickColor.new("Toothpaste");
3IN.Parent = workspace;

Or, you can use keys in a way to index an Instance's children:

1local IN = Instance.new("Part");
2local NewIN = Instance.new("Part", IN);
3NewIN.Name = "New Instance";
4IN.Parent = workspace;
5 
6print(IN['New Instance'].Size); --2, 4, 1 (or something like that)

To index a table using a key, you use the square brackets (note, SQUARE brackets, not parenthesis.)

For example:

Using a Key to make indices within a table:
1local Table = {};
2local value = "Hello, World!"
3 
4Table['key'] = value;
5 
6local MyValue = Table['key'];
Generating a table using hardcoded indices, then getting the value.
1local Table = {
2    one = true;
3    two = false;
4    three = "three";
5}
6 
7local OneValue, TwoValue, ThreeValue = Table['one'], Table['two'], Table['three'];
8print(OneValue, TwoValue, ThreeValue) -->> true, false, three
Using keys within a table itself:
01local Table = {
02    ["Thwack"] = {
03        Damage = 45;
04        Description = "THWACK (nintendo please nerf hero i beg of you)";
05    };
06    ["Whack"] = {
07        Damage = 25;
08        Description = "hero doesn't deserve this power"
09    }
10 
11    ["Inside of Party"] = true;
12};
13 
14 
15print(Table.Thwack, Table['Thwack']); -- table 0xMemoryAddress table 0xMemoryAddress
16print(Table.Whack, Table['Whack']); -- table 0xMemoryAddress table 0xMemoryAddress
17pcall(function()
18    print(Table.Inside Of Party) --errors
19end);
20print(Table["Inside of Party"]); --true

Resources

Be sure to look up resources to further develop your knowledge about tables. Tables are a major contender for one of the most useful basic abilities for a language.

Lua-Users: http://lua-users.org/wiki/TablesTutorial

0
Thanks, it worked! I've never knew you could use keys with tables. BashGuy10 384 — 5y
Ad
Log in to vote
-2
Answered by
karlo_tr10 1233 Moderation Voter
5 years ago
1local value = toolvalues[string]
0
Listen, even if your answer is correct, it doesn't mean you should straight give out code. Please give an explanation on how something works and their uses, and maybe even their caveats. I'm sorry, but I'll have to downvote this answer. There was no effort put into it. Fifkee 2017 — 5y

Answer this question