##Question
How do i index strings and how it works?
##My Answer
I can think of 2 cases what you mean by "index strings."
The first case is *"How do I index /set an index to a dictionary using a string?"
The second case is "How to directly index a string?"
###Answer to first case
You can index a table with any datatype this includes strings, numbers, table, etc.
local dictionary = {
["apple"] = 123,
[212] = "current rep"
}
print(dictionary["apple"])
print(dictionary[212])
dictionary["epic"] = "sinistermemories"
print(dictionary["epic"])
###Answer to second case
Roblox has a sandboxed lua environment. If you are outside of that sandbox you can edit the global string metatable and you can allow strings to be indexed! It sounds crazy but lua has function for it.
local stringMeta = getmetatable("")
local stringMetaIndex = stringMeta.__index
function stringMeta.__index(self, x)
if not stringMetaIndex[x] then
return self:sub(x, x)
end
return stringMetaIndex[x]
end
print(("hello")[1])
2 comments
may i know whats golbal string metatable?? thanks
also dude can u explain to me how to use self:sub() and how it works?cause im really not that good in scripting, thanks