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

How do I index a number from a dictionary?

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
}

1 comment

0

Wouldn't using a table be better for your use case as opposed to a dictionary?

3 answers

✓ Accepted answer

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!

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])

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"])

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