I ran into an error that was caused by mixing up two variables, String, which is a string, and Strings a table. Instead of indexing Strings (the table) I indexed String (the string), which lead to the error “Attempted to index nil with string”
I never realized I was trying to index a string, until I got an AI to figure it out for me… I assumed it was a table since it was getting indexed and returned nil
However, WHY DOES INDEXING A STRING NOT ERROR???
Well, I would understand if you could index a string and get the character at that position in the string, but that isn’t the case, it just returns nil, which leads to more confusion when running into errors like mine
I found this thread while searching, and from the solution, it seems like this is a totally normal behaviour, which is even more confusing for me
(You also have to index a string variable, or use parenthesis, indexing a string directly will error)
local s = "a"
print(s[1]) --> nil
print("a"[1]) --> Expected ')' (to close '(' at column 20), got '['
print(("a")[1]) --> nil
Isn’t String a table ? When you do String.sub() or any other function it’s like when you index a table with . instead of [] ?
I haven’t tested but maybe you can do String["sub"]()
that happens bc strings is a array (table) of chars each char being 1 byte and it make sense if you thought about that is how the computers handle strings (char array/table of chars) in general