I’ve made a table in a ModuleScript that very simply assigns each letter (a-z) to a value (0-25). Very simple and easy to do. However, when I print the value of each number using an in pairs loop, the values are not consecutive. I was expecting for it to just return 0-25 consecutively as that is how I have ordered the table. However, it returns in a weird order, printing an even number then the odd number just below it (0, 2, 1, 4, 3, etc)
This is a reproducible error, I tried shifting the range of numbers to 1-26 and this keeps happening (except in this one the odd numbers print first then the even number below it (1, 3, 2, 5, 4, etc). I have no idea why this is happening or what to do to fix it. I expect it’s either a weird quirk of Luau or me not formatting the table correctly, but I’d ideally like a nudge in the right direction. Thanks!
Here’s a section of the table from a to e, in case it will help:
local letterValues = {
a = 0,
b = 1,
c = 2,
d = 3,
e = 4
}
return letterValues
Dictionaries (in lua, tables without numbered indices and not using ipairs) are never guaranteed to return in the same order in any programming language I know. If you want to get this in order you’ll have to put it into an array, then sort it, then use ipairs.
I don’t know exactly what you want, but if you’re just trying to get a = 0 - z = 25 this is all you need:
My end goal with this is to make a light flicker using a string of letters that then get converted into integers, which is then applied to the light’s brightness value. This part of it is just the early stage that honestly I didn’t expect to get caught up at.
Why use the letter method and not just input numbers you may ask? Simply because I am lazy and the good people at idSoftware made a variety of letter strings on QuakeC around 1996, and they produce a very nice end result.
Tbh it may be much easier to use integers and skip the the letter part entirely. I don’t know honestly, I guess this is a “fun” challenge to see if I can do it this way and then I’ll figure out what’s easiest and most efficient later on. Thanks for this though, every bit of knowledge and advice helps me get better and get closer to getting this to work.
Edit: Turns out using integers was much, much easier. I’ve got it working exactly how I want it to in around about half an hour, instead of taking ages working out this conversion between letters and numbers.