How to get next index from dictionary

Hello! I have a dictionary and I need to get the next key of a dictionary using a key. But it returns nil. Please help:


module.damage = {
	["10"] = 2500;
	["20"] = 5000;
	["30"] = 15000;
	["40"] = 35000;
	["50"] = 50000;
	["60"] = 75000;
	["70"] = 125000;
	["80"] = 300000;
	["90"] = 475000;
	["115"] = 750000;
	["135"] = 1050000;
	["175"] = 1350000;
	["225"] = 1850000;
	["275"] = "MAX";

}
print(next(module.damage, "10")) -- it prints nil 
print(next(module.damage, "20")) -- it prints 30, which is correct
print(next(module.damage, "30")) -- it prints 80
print(next(module.damage, "40")) -- it prints 50, which is correct
print(next(module.damage, "50")) -- it prints 135

As you can see, most of them are all random.
If you read this, PLEASE HELP ME. Thank you so much!!

1 Like

https://www.lua.org/manual/5.1/manual.html#lua_next

Sorry, I don’t understand it. Can you see where is my problem in this code?

https://developer.roblox.com/en-us/api-reference/lua-docs/Lua-Globals

Returns the first key/value pair in the array. If a lastKey argument was specified then returns the next element in the array based on the key that provided. The order in which the indices are enumerated is not specified, even for numeric indices. To traverse a table in numeric order, use a numerical for loop or ipairs.
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may, however, modify existing fields. In particular, you may clear existing fields.

So print(next(module.damage)) would return the first key & value pair of the β€œmodule.damage” table.

Does next() work with dictionaries?

The next function has 2 parameters the first of those is represents the table the next() function is acting upon and is required, the second which is optional represents some key of that table of which you wish to retrieve the associated value of.

Yes, try print(next(module.damage, [β€œ275”])) for example.

ok let me try β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž

It prints nil β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž

Please help. :frowning: β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž β€Žβ€Žβ€Ž

Ok I tried the second index it seemed to work. Maybe next() doesn’t work with the first index since i was trying to do it with the first index. But I tried to add something in front of the [β€œ10”] = 2500 and it still returns nil. Please help :frowning:

1 Like

I found out that next() is just as unorderly as pairs() Can someone please help me get the next index from a dictionary please??? Thank you

1 Like

maybe something like this :sweat_smile:

module.damage = {...}
local orderListKeys = {}
function GetKeys(t)
	for i,v in pairs(t) do
		table.insert(orderListKeys,tonumber(i))
	end
	table.sort(orderListKeys)
	print("--------------------------")
	for i,v in pairs(orderListKeys) do
		print(i,v)
	end
	print("--------------------------")
end
GetKeys(module.damage)

local currentValue = "10" --I think you mean currentLevel
local currentIndex = table.find(orderListKeys,tonumber(currentValue)) 
print("currentIndex",currentIndex)

local nextIndex = currentIndex + 1 
print("nextIndex",nextIndex)

local nextValue = orderListKeys[nextIndex]
print("nextValue",nextValue)

print("nextDamage",module.damage[tostring(nextValue)])

1 Like

Omg just use my solution…

local keys = getKeys(module.healths)
for i, k in ipairs(keys) do
    local v = module.healths[k]
    local nextKey = keys[i+1]
    print(k, v, nextKey)
end
2 Likes

If you’re still looking, you need to keep a separate array of dictionary keys, it’s important. I’ve said it before, dictionaries in Lua don’t have any order to them. Pairs and next are pretty close to the same thing by the way:

local function pairs(inputTable)
    return next, inputTable
end

You can give the code I gave in my original solution a try.

module.healths = {
	["100"] = 5000;
	["110"] = 15000;
	["120"] = 35000;
	["130"] = 50000;
	["145"] = 75000;
	["160"] = 125000;
	["185"] = 300000;
	["200"] = 475000;
	["225"] = 750000;
	["255"] = 1050000;
	["280"] = 1350000;
	["320"] = 1850000;
}
module.healthsIndex = {}
for key, value in pairs(module.healths) do
    table.insert(module.healthsIndex, key)
end
table.sort(module.healthsIndex, function(a, b) return module.healths[a] < module.healths[b] end) -- Sort by value rather than key

module.healthsNext = function(key)
    local index = table.find(module.healthsIndex, key)
    if index then
        local newKey = module.healthsIndex[index+1]
        if newKey then return newKey, module.healths[newKey]
        else return "NaN", "MAX" end -- If there is nothing larger, return the key NaN and the value MAX
    else
        warn(("Unable to find key \"%s\" in module.healthsIndex"):format(key))
    end
end

print(module.healthsNext("120")) --> 130, 50000
1 Like

Thank you so much! Even though I found a solution on my own already just marked it as solution mark. Thanks for you time!

1 Like