Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

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

0

Yeah, that works, but then how do I find where it starts and ends?

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.