Second “Helpful” post of the day, Here we go, Alright, so I made a module that transforms text into morse, look
-- MorseCodeModule
local MorseCodeModule = {}
-- Morse code dictionary
local morseCode = {
["A"] = ".-", ["B"] = "-...", ["C"] = "-.-.", ["D"] = "-..",
["E"] = ".", ["F"] = "..-.", ["G"] = "--.", ["H"] = "....",
["I"] = "..", ["J"] = ".---", ["K"] = "-.-", ["L"] = ".-..",
["M"] = "--", ["N"] = "-.", ["O"] = "---", ["P"] = ".--.",
["Q"] = "--.-", ["R"] = ".-.", ["S"] = "...", ["T"] = "-",
["U"] = "..-", ["V"] = "...-", ["W"] = ".--", ["X"] = "-..-",
["Y"] = "-.--", ["Z"] = "--..",
["1"] = ".----", ["2"] = "..---", ["3"] = "...--", ["4"] = "....-",
["5"] = ".....", ["6"] = "-....", ["7"] = "--...", ["8"] = "---..",
["9"] = "----.", ["0"] = "-----",
[" "] = " "
}
function MorseCodeModule.textToMorse(text)
local morseCode = ""
local uppercaseText = string.upper(text)
for char in uppercaseText:gmatch("[%a%s]") do
morseCode = morseCode .. (morseCodeTable[char] or "")
if char ~= " " then
morseCode = morseCode .. " "
end
end
return string.match(morseCode, "^%s*(.-)%s*$")
end
function MorseCodeModule.morseToText(morseCode)
local decodedText = ""
local words = {}
for word in morseCode:gmatch("[^/]+") do
local decodedWord = ""
for char in word:gmatch("[^%s]+") do
for key, value in pairs(morseCodeTable) do
if value == char then
decodedWord = decodedWord .. key
break
end
end
end
table.insert(words, decodedWord)
end
decodedText = table.concat(words, " ")
return decodedText
end
return MorseCodeModule
Example :
local MorseCodeModule = require(game:GetService("ServerScriptService").MorseCodeModule)
local plainText = "Hello World!"
local encodedMorseCode = encodeMorseCode(plainText)
print(encodedMorseCode)
local decodedText = decodeMorseCode(encodedMorseCode)
print(decodedText)
Thanks for the amazing contribution! @vxsqi