How can I detect what word a player is typing on a Textbox?

Like for example, in the picture below, I want to make it so that the word “If” would be highlighted after I press space (since my current word is free.)

How would I be able to do this? To find out what current player’s word is and then highlight it.
I thought of using string.match but some words like ‘he’ are going to be used repeated, so that sounds problematic.

I think :GetPropertyChangedSignal("Text") would be your best friend for this,

I’m not too sure on any other ways

Yes, I’ve already using that for the matching. Right now the problem is about highlighting a text in the TextLabel based on the input word.

This might work but I’m not too sure

words = {
  "Hello",
  "World.",
  "How",
  "Are",
  "You?"
}

TextLabel:GetPropertyChangedSignal("Text"):Connect(function()
  local text = TextLabel.Text
  local splitText = text:split(" ")

  local word = splitText[#splitText]

  if word == words[#splitText] then
    -- he did it right
  else
    -- fail
  end
end)
local TextToType = "This is a test message."
local TextLabel = script.Parent.Parent.TextLabel
local TextBox = script.Parent




local ToTypeSplit = string.split(TextToType, " ")
local LastValue = nil
TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	local CurrentText = TextBox.Text
	local CurrentSplit = string.split(CurrentText, " ")
	if #CurrentSplit > #ToTypeSplit then
		TextLabel.Text = '<font color="rgb(147, 0, 0)">'..table.concat(ToTypeSplit, " ", 1, LastValue).."</font>".." "..table.concat(ToTypeSplit, " ", LastValue+1)
		return
	end
	if #CurrentSplit == #ToTypeSplit then
		TextLabel.Text = '<font color="rgb(255,165,0)">'..TextToType.."</font>"
	else
		TextLabel.Text = '<font color="rgb(255,165,0)">'..table.concat(ToTypeSplit, " ", 1, #CurrentSplit).."</font>".." "..table.concat(ToTypeSplit, " ", #CurrentSplit+1)
	end
	LastValue = #CurrentSplit
end)

Test.rbxl (26.1 KB)

Something like this?

Close, but it needs to be per character. My bad, forgot to specify that. Something like this:

My script looks like this:


raceFrame.TextFrame.TypingBox:GetPropertyChangedSignal("Text"):Connect(function()
	latestWord = raceFrame.TextFrame.TypingBox.Text
	print(latestWord, currentWord)
	if latestWord == currentWord then
		if #currentQuote == 1 then
			table.remove(currentQuote, 1)
			raceFrame.TextFrame.TypingBox.Text = ""
			raceFrame.TextFrame.TypingBox.TextEditable = false
			raceFrame.TextFrame.TypingBox:ReleaseFocus()
			displayResults()	
		elseif #currentQuote == 2 then
			table.remove(currentQuote, 1)
			currentWord = currentQuote[1]
			raceFrame.TextFrame.TypingBox.Text = ""
		else
			table.remove(currentQuote, 1)
			currentWord = currentQuote[1].." "
			raceFrame.TextFrame.TypingBox.Text = ""
		end
	end
end)

If you keep track of what word they’re on from the start, you should be able to just increment that whenever they press space. Once you know what word they’re on, you can check if truncating that word gives what they’ve typed so far, and if so, do the green highlight effect.

2 Likes

I’ve modified it and put a count, but how exactly will I be able to concatenate the strings in green and normal colour?

raceFrame.TextFrame.TypingBox:GetPropertyChangedSignal("Text"):Connect(function()
	local currentCount = 0
	latestWord = raceFrame.TextFrame.TypingBox.Text
--	print(latestWord, currentWord)
	local splittedLatest = string.split(latestWord)
	local spllitedCurrent = string.split(currentWord)

	if latestWord == currentWord then
		currentCount += 1
		if #currentQuote == 1 then
			table.remove(currentQuote, 1)
			raceFrame.TextFrame.TypingBox.Text = ""
			raceFrame.TextFrame.TypingBox.TextEditable = false
			raceFrame.TextFrame.TypingBox:ReleaseFocus()
			displayResults()	
		elseif #currentQuote == 2 then
			table.remove(currentQuote, 1)
			currentWord = currentQuote[1]
			raceFrame.TextFrame.TypingBox.Text = ""
			
		else
			table.remove(currentQuote, 1)
			currentWord = currentQuote[1].." "
			raceFrame.TextFrame.TypingBox.Text = ""
		end
		
	end

end)

Just create a table where you store correct and incorrect. Insert the start and end of every word and then get the start of the first word and the end of the last word. Then you know how much to highlight as correct and then for incorrect you can do the same but you just store one value instead of multiple ones.

send a download link for this script thing