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

How do I index a number from a dictionary?

Asked by 6 years ago

I am trying to add a number from a dictionary, but how do I get an individual number indexed?

Indexing:

1Player.Character:WaitForChild('RunningSpeed').Value = 25 + (KillerStats[1])

Dictionary

1local KillerStats = {
2    Speed = 1,
3    Damage = 3,
4    Vision = 10
5}
0
Wouldn't using a table be better for your use case as opposed to a dictionary? iiOmqLeetHyackth101 0 — 6y

3 answers

Log in to vote
1
Answered by
zblox164 531 Moderation Voter
6 years ago

You don't index dictionary's with integers (whole numbers) instead you index them with the name you gave the index to be.

Example:

1local diction = {
2    ["Index"] = "Some Value";
3    ["Something"] = "Another value"
4}
5 
6for i, v in pairs(diction) do
7    print(i..v) -- outputs "Index" and "Something" along with their values
8end

So you know the semi colon is the same as using a comma

Lets go back to the example you had. Your index is different then mine but here's how you index what you want from your dictionary.

01local KillerStats = {
02    -- I put brackets on your index
03 
04    ["Speed"] = 1,
05    ["Damage"] = 3,
06    ["Vision"] = 10
07}
08 
09print(KillerStats.Speed) -- or
10print(KillerStats["Speed"])
11-- Should output 1 (*2)

Hope this helps!

Ad
Log in to vote
1
Answered by
yellp1 193
6 years ago
Edited 6 years ago

I haven't done much with dictionaries yet but to my understanding it's just like looking up the word in a dictionary. You don't search it by list because it's not in a numbered list. You would search it by name and then get the definition (Value).

1Player.Character:WaitForChild('RunningSpeed').Value = 25 + (KillerStats[Damage])
Log in to vote
1
Answered by 6 years ago

The problem is that what you assigned isn't doesn't become the original index anymore; using a dictionary you need to specify your custom index for roblox to examine and verify it. For example,

1--Dictionary
2local KillerStats = {
3    ["Speed"] = 1,
4    ["Damage"] = 3,
5    ["Vision"] = 10
6}
1--Script
2Player.Character:WaitForChild('RunningSpeed').Value = 25 + (KillerStats["Speed"])

Answer this question