[HELP!] How Do I Find Where A Certain Char In A String Is?

:exclamation:I’m trying to make a wordle game in Roblox Studio, but the system doesn’t know when to put a letter as green (in the right place of the word you’re guessing), but It can easily put the letter in yellow (not in right place of guessing letter, but letter is in the word).
image
:exclamation:As you can see in this image, the word is KEYS. This means the K in the image is supposed to be green, but is yellow. (the yellow, and black letters are correct)
:exclamation:SCRIPT:

local btn = script.Parent

local A1 = script.Parent.Parent.Parent.Words.A1
local A2 = script.Parent.Parent.Parent.Words.A2
local A3 = script.Parent.Parent.Parent.Words.A3
local A4 = script.Parent.Parent.Parent.Words.A4

local HWTxt = script.Parent.Parent.Parent.Parent.HIDDENwordTXT

local combinedGuess = tostring(A1.Text..A2.Text..A3.Text..A4.Text)

btn.MouseButton1Click:Connect(function()
	print(combinedGuess)
	if A1.Text ~= "" and A2.Text ~= "" and A3.Text ~= "" and A4.Text ~= "" then
		for i = 1,4 do
			local target = nil
			wait(0)
			if i == 1 then target = A1
			elseif i == 2 then target = A2
			elseif i == 3 then target = A3
			elseif i == 4 then target = A4
			end
			wait(0)
			if string.find(tostring(HWTxt.Text),tostring(target.Text)) then
				target.TextColor3 = Color3.fromRGB(255, 255, 0)
                                 --to make the guessing letter yellow
				wait(0)
                                --here's where the green check starts!
				if tostring(HWTxt.Text):sub(i) == tostring(target.Text) then
					target.TextColor3 = Color3.fromRGB(0, 255, 0)
                                        --to make the guessing letter green
				else
					print(tostring(HWTxt.Text):sub(i),tostring(target.Text),i)
                                        --output >>
                                        --KEYS K 1
                                        --EYS Y 2
                                         --YS E 3
--[CORRECT WORD]'s char in order of what # **i** is TO [CHAR THAT PLR TYPED]
				end
			end
		end
	end
end)

Please help! :cry:

4 Likes
--This function may be more optimal for huge strings
--This is because find does most of the work
function findMultiple(s1: string, c: string, plain: boolean?): {number}
	local indexes, index = {}, 0
	while index do
		index = s1:find(c, index+1, plain)
		table.insert(indexes, index)
	end
	return indexes 
end

--This function is short and simple if you plan to use it for small strings
function getCharPos(s1: string, c: string): {number}
	local indexes = {}
	for i = 1, s1:len() do
		if s1:sub(i, i) == c then table.insert(indexes, i) end
	end
	return indexes 
end

--Should both give {3, 4, 10}
--Those are the positions of the letter "l" in "Hello world!"
print(findMultiple("Hello world!", "l"))
print(getCharPos("Hello world!", "l"))
1 Like

To find a certain part of a string, use string.match(str, str).

In this example, string.match("keys", "k").
It will return true or false, if it returns true, set K’s TextColor3 to Green.

Yes, but how would I see if “k” is in the same place as the “k” in “keys” (or if not) ?

string.find() returns the start and end position of a substring. If you use it with a single character, it can return where it is.

This should work. Replace the number with whatever column the letter is in, and if it matches, run your code in the your code here block.

if string.split("keys", "")[1] == "k" then
    -- your code here
end

Thank you, this works! :+1::+1::+1::+1::+1::+1::+1::+1:

1 Like

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