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

is this the "propper" way to find a key?

i have a table like this

local levs = {}

local maxlev = 50
local loops  = maxlev
local level = 1
local xp = 5

while loops > 0 do
	loops = loops - 1
	levs[level] = xp
	xp = math.floor(xp + (xp / 2))
	level = level + 1
end

this makes a table like

[1] = 5
[2] = 7
[3] = 10
[4] = 15
[5] = 23

and so on up to 50 im now needing to compare a value to the table and find a key that matches it for example if the input is 9 it ill read the table and find the last key that has a value less than 9 so in this case they key of 7 so it would return 2 and if the input was 16 it would return 4

i currently have this

_G.XPCheck = function(XP)
	local keycheck = 1
	if levs[keycheck] < XP then 
		keycheck = keycheck + 1
	else 
		return keycheck - 1
	end
end

is there a more propper way of doing this?

4 comments

0

I think so. I am answering your question

0

How would you do this?

0

almost done

0

Why are you setting numerical indexes for your dictionary? This could be hazardous in some cases and in your case its useless since you're defining in chronological order.

2 answers

✓ Accepted answer

Yes, it’s also best to write your Scripts in a more compatible manner. For example here, we’d strive to ibe less dependent on the requested element being less than the given value by 1, and more of the closest, lower value instead.

To fully achieve your goal, it’s actually best to iterate backwards through the array. This way we can identify if the indexed element is equivalent to the given number, then return it, if not, return the one below it as it’ll be the first element likely to be lower. This also reduces Conditionals if you use another way.

local array = {
   [1] = 5
   [2] = 7
   --// and so on
};

function retrieveClosestOr(array, number)
   for i = #array, 1, -1 do
      if (arr[i] <= number) then return array[index] end
   end
   return nil
end

Hope this helps, if so don’t forget to accept!

Ok, so I personally think that the best way to check a table is to use a for key, value do loop. It is great for iterating through a table.

local levs = {
[1] = 5;
[2] = 7;
[3] = 10;
[4] = 15;
[5] = 23;
}
    
    local XP = 10
    for Key, Value in pairs(levs) do
		wait(1)
	    if Value == XP then
			print("Correct Key! Key number "..Key.." is the right key!") 
	    else
			print("Wrong Key! Key number ".. Key.." is not the right key!")

		end
    end


Just one tiny problem: I don't know how to round the XP to the smaller value. I will work on that and try to figure it out.

3 comments

0

no

0

stop abusing if you go on the roblox dev hub it says the best way to iterate a dictionary is to use a key value loop

0

Why are you downvoting? I may have not fully answered his question, but I gave him a base to start with.

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