How do I fix the text background color being incorrect?

Hello, I’m trying to make a simple game almost like the game ‘Wordle.’ Any who, I’m having this problem with the background colors of the text being incorrect. They all have the same prompt. The prompt and correct word they are trying to find is the word…

EARTH

(Note, if you haven’t played Wordle, the letters that are green are in the correct position, letters that are orange are also correct but are in the wrong position, and gray letters are not in the word at all.)

PROBLEMS

1. The guessing word “FAITH” has the letters ‘A’, ‘T’, and ‘H’ shown to be correct, yet that isn’t right, the letter ‘F’ isn’t in the word we are looking for either.

It should look more like something like this:

(From a custom Wordle.)
image

2. The word “MOURN” has only one letter that is correct but wrong position in the guessing word. And obviously ‘M’ isn’t in the word “EARTH” but the letter ‘R’.

This guessing word should also look something like this:
image

Here’s the main local script code for the text updating:
The main function that should be focused on is the updateWordSets() function.

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")

local textBox = script.Parent.Background.MainScroll.InputWordFrame.Input
local submitEvent = ReplicatedStorage:WaitForChild("SubmitFeedback")

local function handleInput(input)
	local newText = ""

	for char in input:gmatch(".") do
		if #newText < 5 then
			if char:match("[A-Za-z]") then
				newText = newText .. char:upper()
			end
		end
	end

	textBox.Text = newText
end

local function sendWordToServer(word)
	if #word == 5 then
		submitEvent:FireServer(word)
	else
		local errorSound = Instance.new("Sound", SoundService)
		errorSound.SoundId = "rbxassetid://550209561"
		errorSound:Play()

		errorSound.Ended:Connect(function()
			errorSound:Stop()
			errorSound:Destroy()
		end)
	end
end

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	handleInput(textBox.Text)
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Return then
		local word = textBox.Text
		sendWordToServer(word)
	end
end)

local function updateWordSets(correctPositionLetters, incorrectPositionLetters, incorrectLetters, word)
	local wordSet = script.Parent.Background.MainScroll:FindFirstChild("WordSet1")
	if wordSet then
		local guessLabels = wordSet:GetChildren()

		local filteredLabels = {}
		for _, label in ipairs(guessLabels) do
			if label:IsA("TextLabel") and label.Name:find("Guess") then
				table.insert(filteredLabels, label)
			end
		end

		table.sort(filteredLabels, function(a, b)
			return a.LayoutOrder < b.LayoutOrder
		end)

		local wordLength = #word

		for i, guesslabel in ipairs(filteredLabels) do
			local guessChar = word:sub(i, i)

			guesslabel.Text = guessChar

			if i <= correctPositionLetters then
				guesslabel.BackgroundColor3 = Color3.fromRGB(150, 255, 125)
				print("Correct position letter:", guessChar, "at index:", i)
			elseif i <= correctPositionLetters + incorrectPositionLetters then
				guesslabel.BackgroundColor3 = Color3.fromRGB(255, 175, 105)
				print("Incorrect position letter:", guessChar, "at index:", i)
			elseif i <= wordLength then
				guesslabel.BackgroundColor3 = Color3.fromRGB(155, 155, 155)
				print("Letter not in it:", guessChar, "at index:", i)
			end
		end
	end
end

submitEvent.OnClientEvent:Connect(updateWordSets)

Server Script if that’s for any needing:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local wordList = {"EARTH"}

local submitEvent = ReplicatedStorage:FindFirstChild("SubmitFeedback")

local chosenWord = wordList[math.random(1, #wordList)]
print("Chosen word:", chosenWord)

local function checkWord(guess, target)
	local correctPositionLetters = 0
	local incorrectPositionLetters = 0
	local incorrectLetters = 0
	local correctLetters = ""
	local incorrectPositionLettersLetters = ""
	local incorrectLetters = ""
	local targetLetters = {}
	local guessLetters = {}

	for i = 1, #target do
		local letter = target:sub(i, i)
		targetLetters[letter] = (targetLetters[letter] or 0) + 1
	end

	for i = 1, #guess do
		local guessLetter = guess:sub(i, i)
		local targetLetter = target:sub(i, i)

		if guessLetter == targetLetter then
			correctPositionLetters = correctPositionLetters + 1
			targetLetters[guessLetter] = targetLetters[guessLetter] - 1
			correctLetters = correctLetters .. guessLetter
		else
			guessLetters[guessLetter] = (guessLetters[guessLetter] or 0) + 1
			if targetLetters[guessLetter] and targetLetters[guessLetter] > 0 then
				incorrectPositionLetters = incorrectPositionLetters + 1
				targetLetters[guessLetter] = targetLetters[guessLetter] - 1
				incorrectPositionLettersLetters = incorrectPositionLettersLetters .. guessLetter
			elseif guessLetters[guessLetter] <= (targetLetters[guessLetter] or 0) then
			else
				incorrectLetters = incorrectLetters .. guessLetter
			end
		end
	end

	return correctPositionLetters, incorrectPositionLetters, incorrectLetters, correctLetters, incorrectPositionLettersLetters
end

local function handleWordSubmission(player, word)
	local correctPositionLetters, incorrectPositionLetters, incorrectLetters = checkWord(word, chosenWord)
	print("Correct position letters:", correctPositionLetters)
	print("Incorrect position letters:", incorrectPositionLetters)
	print("Incorrect letters:", incorrectLetters)
	submitEvent:FireClient(player, correctPositionLetters, incorrectPositionLetters, incorrectLetters, word)
end

submitEvent.OnServerEvent:Connect(handleWordSubmission)
1 Like

I think I’ve figured out what’s causing that to happen, and it largely stems from two places:

  1. This section of code toward the end of the “updateWordSets” function in the first codeblock:
  1. This conditional statement from the server script (in the second codeblock)

If I understand correctly, when it reaches the condition of if i <= correctPositionLetters, that is checking if the current iteration of the loop is less than or equal to the value of “correctPositionLetters”.

However, the issue here is that the “correctPositionLetters” variable appears to be based on how many letters in the target word were guessed correctly, rather than defining which specific positions in the word were deemed correct by the server script.


As a result, the “updateWordSets” function only ever highlights the letters in the guessed word from left to right because as it iterates through each letter of the word, “i” increases by 1, so once it increases to the point where it exceeds the value of “updateWordSets”, it will no longer highlight a guessed letter as correct.

Just in case my explanation didn’t make much sense (or to at least verify with you if I’m understanding how these scripts are working together), I’ll breakdown the first example you provided:


Target word: Earth

Guessed word: Faith

  • The server script loops through the entirety of the guessed word, letter by letter. It checks if guessLetter == targetLetter.

    • On iteration 2, it sees that the letter “a” matches, so it increases “correctPositionLetters” by 1.

    • On iterations 4 and 5, it sees that the letters “t” and “h” matches, so it increases the value of the variable by 1 each time, meaning that “correctPositionLetters” now equals 3.

  • After the server script has analyzed the guessed word, it returns the “correctPositionLetters” along with several other values. The LocalScript / first codeblock then looks through the TextLabels that contain the letters of the guessed word. It sorts it into the order that the word was spelled, then loops through each one, checking if the current iteration of the loops is less than or equal to the value of “correctPositionLetters”.

    • Because “correctPositionLetters” equals 3, the first 3 iterations of the loop will meet the first conditional statement, meaning:
      • Iteration #1 is the letter F: Marked as correct

      • Iteration #2 is the letter A: Marked as correct

      • Iteration #3 is the letter I: Marked as correct

      • Iteration #4 is the letter T: Marked as incorrect because the iteration number is greater than “correctPositionLetters”

      • Iteration #5 is the letter H: Marked as incorrect for the same reason as above


In order to resolve this, you could instead change the “correctPositionLetters” variable to a table that contains the positions within the word that were correctly guessed (or something similar to it that lets you clearly track which position in the word was guessed correctly):

-- Example revision of the server script code

correctPositionLetters = {}

...

if guessLetter == targetLetter then
    correctPositionLetters[i] = guessLetter -- Sets the index to the current position of the letter within the guessed word
 -- Example revision of the LocalScript code

if guessLabel.Text == correctPositionLetters[i] then -- Looks through the table based on the current iteration of the loop (which should match up with position of the letter in the guessed word)
    -- Continue because we know the letter in the current Text of the TextLabel matches the letter that was marked as correct in the exact same position within the word (because the iteration of the loop matches the position of the letter in the word)

Edit: I forgot to mention this, but the same format would apply for the other conditional statements so that you can also keep track of the letters that were either correct but in the wrong spot or completely incorrect so that you can update the background colors accordingly.

However, I didn’t fully analyze what goes on in the else statement after the “guessLetter == targetLetter” condition is checked for in the server script, so maybe this proposed solution might not be easy to implement; I hope that this provides a starting point, at least!


  • Alternatively you could reference the “correctLetters” value that was sent from the server script, then use string.gmatch() to see if the letter within the current TextLabel matches any of the letters in that variable.

    • If it matches one of the letters, then the background color of the TextLabel could be updated to indicate that it was correct.

      • However, in the case of words with multiple of the same letter, you would also need to know where in the word each of the “correctLetters” were, so the solution I proposed in the codeblocks above with storing the correctly guessed letter and its position in the word in a table might be more a intuitive option.

Hopefully this helps! :grin:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.