So I have a textbox and a textbutton.
When I click the textbutton I want the script below to check where a word begins and ends
But I get an error: attempted to index local Word (a number value)
How do I fix this?
function onClicked() local Text = script.Parent.Parent.TextBox.Text local Word = string.find(Text, "test") local StartOfWord = Word[1] local EndOfWord = Word[2] print(EndOfWord) print(StartOfWord) end script.Parent.MouseButton1Click:Connect(onClicked)
You cannot index a string. Use Text:sub(position, position)
(or string.sub(Text, position, position)
) to get the behavior you want (find character at that position). Plus, string.find()
returns two number values (starting and ending position of the text in the phrase), you want string.match()
to get the text.
You can find more information on string manipulation here.
Edit: If what you wanted was to get the starting and ending location, you could fix it by replacing line 3 to have Word
be a table: local Word = {string.find(Text, "test")}
(This will put the two returned values into a table, meaning you can index it with Word[1]
and Word[2]
.)