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 5 years ago

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

Indexing:

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

Dictionary

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

3 answers

Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 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:

local diction = {
    ["Index"] = "Some Value"; 
    ["Something"] = "Another value"
}

for i, v in pairs(diction) do
    print(i..v) -- outputs "Index" and "Something" along with their values
end

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.

local KillerStats = {
    -- I put brackets on your index

    ["Speed"] = 1,
    ["Damage"] = 3,
    ["Vision"] = 10
}

print(KillerStats.Speed) -- or
print(KillerStats["Speed"])
-- Should output 1 (*2)

Hope this helps!

Ad
Log in to vote
1
Answered by
yellp1 193
5 years ago
Edited 5 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).

Player.Character:WaitForChild('RunningSpeed').Value = 25 + (KillerStats[Damage])
Log in to vote
1
Answered by 5 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,

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

Answer this question