Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why can't I index the results of string.find?

Asked by 5 years ago

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)
0
line 3 isn't right, you need to define word another way..The format would be more like ~if string.find(Text, "test") then..end~ ABK2017 406 — 5y
0
Yeah, that works, but then how do I find where it starts and ends? jgoesgaming 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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].)

Ad

Answer this question