Decoder Ordering Final Text Going Wrong

I got part of my decoder fixed earlier this morning, but a new problem started happening. Basically my decoder should decode this pattern (Q = A, W = B, E = C…) but it decodes in the wrong order. (Following the pattern, REW should translate to DCB but instead shows BCD).
Here is my code:

local decoder = require(game.ReplicatedStorage.ClientDecoder)
local textbox = script.Parent.TextBox
local textbutton = script.Parent.TextButton
local textlabel = script.Parent.TextLabel
textbutton.MouseButton1Click:Connect(function()
	print("CLICKED")
	local finaltext = ""
	local text = textbox.Text
	for j = 1, 26 do
		local key = decoder.KeyboardUp[j]
		local uppercase = decoder.UpperKeywords[j]
		local lowercase = decoder.LowerKeywords[j]
		for i in string.gmatch(text, ".") do
			if i == key then
				if finaltext == "" then
					finaltext = uppercase
				else
					finaltext = finaltext .. uppercase
				end
			end
		end
	end
	textlabel.Text = finaltext
end)

Thanks for all your help!

I think the issue is that you’re iterating through every character in the string, 26 times. That seems like a mistake.

You could use gsub for this to do the loop for you:

local function Decode(text, cipher)
	local repl = text:gsub(".", function(char)
		return cipher[char]
	end)
	
	return repl
end


print(
	Decode("Hezzo Forzd!", {
		z = "l",
		F = "W",
	})
) --> Hello World!

But it would still be fine if the loop positions would be reversed? (The string.gmatch loop outside and the 26 times loop inside)

Yeah if you wanna do it like you are, the loops should be swapped. The 1-26 should be inside.

Right now you’re looping through the cipher and appending any characters that match the cipher char.

1 Like

Thanks! I should have analyzed more, but I guess I still have a lot to learn.