Trying to get the exact next value in a dictionnary

Hello there !

I’m trying to find the exact next level inside a dictionnary in my script :

function update()
		for key,valueindico in pairs(roles) do
			
			if key > val.Value then
				print("trouvé "..key)
				script.Parent.Text = valueindico
				break
			end
		end
	end

^This loop actually returns me any results higher than my amount of XP. :confused:

Let’s say I have 300 XP (level 3) and my next level is at 320 XP, I would like the loop to return "Level 4 ", not level 5 or anything, regardless of the amount of XP I have between both levels.

How can I do that ?

Thanks already for your support !

You need to loop through the entire dictionary to find the smallest value that is still bigger than the current XP. The way you have it written now will only find the first value in the dictionary that is higher than your XP, which is not necessarily the smallest.

Try this:

function update()
  local smallest = math.huge --start at the biggest possible number
  for key, value in roles do
    --value should be higher than XP but smaller than the current smallest one
    if key > val.Value and key < smallest then
      smallest = key
    end
  end
  print(smallest, roles[smallest]) --the result
end

Yep it worked, thanks for sorting that out !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.