Advertisement
KrYn0MoRe

morse code translator/player

Sep 21st, 2021 (edited)
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local chart = {
  2.     --
  3.     ['a'] = {0,1},
  4.     ['b'] = {1,0,0,0},
  5.     ['c'] = {1,0,1,0},
  6.     ['d'] = {1,0,0},
  7.     ['e'] = {0},
  8.     ['f'] = {0,0,1,0},
  9.     ['g'] = {1,1,0},
  10.     ['h'] = {0,0,0,0},
  11.     ['i'] = {0,0},
  12.     ['j'] = {0,1,1,1},
  13.     ['k'] = {1,0,1},
  14.     ['l'] = {0,1,0,0},
  15.     ['m'] = {1,1},
  16.     ['n'] = {1,0},
  17.     ['o'] = {1,1,1},
  18.     ['p'] = {0,1,1,0},
  19.     ['q'] = {1,1,0,1},
  20.     ['r'] = {0,1,0},
  21.     ['s'] = {0,0,0},
  22.     ['t'] = {1},
  23.     ['u'] = {0,0,1},
  24.     ['v'] = {0,0,0,1},
  25.     ['w'] = {0,1,1},
  26.     ['x'] = {1,0,0,1},
  27.     ['y'] = {1,0,1,1},
  28.     ['z'] = {1,1,0,0},
  29.     --
  30.     ['1'] = {0,1,1,1,1},
  31.     ['2'] = {0,0,1,1,1},
  32.     ['3'] = {0,0,0,1,1},
  33.     ['4'] = {0,0,0,0,1},
  34.     ['5'] = {0,0,0,0,0},
  35.     ['6'] = {1,0,0,0,0},
  36.     ['7'] = {1,1,0,0,0},
  37.     ['8'] = {1,1,1,0,0},
  38.     ['9'] = {1,1,1,1,0},
  39.     ['0'] = {1,1,1,1,1},
  40.     --
  41.     ['.'] = {0,1,0,1,0,1},
  42.     [','] = {1,1,0,0,1,1},
  43.     ['?'] = {0,0,1,1,0,0},
  44.     ['\''] = {0,1,1,1,1,0},
  45.     ['!'] = {1,0,1,0,1,1},
  46.     ['/'] = {1,0,0,1,0},
  47.     [':'] = {1,1,1,0,0,0},
  48.     [';'] = {1,0,1,0,1,0},
  49.     ['='] = {1,0,0,0,1},
  50.     ['+'] = {0,1,0,1,0},
  51.     ['-'] = {1,0,0,0,0,1},
  52.     ['_'] = {0,0,1,1,0,1},
  53.     ['"'] = {0,1,0,0,1,0},
  54.     ['@'] = {0,1,1,0,1,0},
  55.     --
  56. }
  57.  
  58. local function to_morse(str)
  59.     if str and #str >= 1 then else return end
  60.     str = string.gsub(str,' ','.')
  61.     local data = {}
  62.     for i = 1,#str do
  63.         local letter = string.sub(str,i,i)
  64.         local beeps = chart[letter]
  65.         if beeps then
  66.             table.insert(data,beeps)
  67.         end
  68.     end
  69.     return data
  70. end
  71.  
  72. local plr = owner
  73. local char = plr.Character
  74. local head = char:FindFirstChild("Head")
  75.  
  76. BillboardGui0 = Instance.new("BillboardGui")
  77. TextBox1 = Instance.new("TextBox")
  78. BillboardGui0.Parent = head
  79. BillboardGui0.LightInfluence = 0
  80. BillboardGui0.Brightness = 2
  81. BillboardGui0.Size = UDim2.new(8, 0, 3, 0)
  82. BillboardGui0.Active = true
  83. BillboardGui0.ClipsDescendants = true
  84. BillboardGui0.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  85. BillboardGui0.SizeOffset = Vector2.new(0, 2)
  86. TextBox1.Parent = BillboardGui0
  87. TextBox1.Size = UDim2.new(1, 0, 1, 0)
  88. TextBox1.BackgroundColor = BrickColor.new("Institutional white")
  89. TextBox1.BackgroundColor3 = Color3.new(1, 1, 1)
  90. TextBox1.BackgroundTransparency = 1
  91. TextBox1.Font = Enum.Font.SourceSans
  92. TextBox1.FontSize = Enum.FontSize.Size14
  93. TextBox1.TextColor = BrickColor.new("Institutional white")
  94. TextBox1.TextColor3 = Color3.new(1, 1, 1)
  95. TextBox1.TextScaled = true
  96. TextBox1.TextSize = 14
  97. TextBox1.TextStrokeTransparency = 0
  98. TextBox1.TextWrap = true
  99. TextBox1.TextWrapped = true
  100.  
  101. local last_change = 0
  102.  
  103. local function change_text(str)
  104.     last_change = os.clock()
  105.     TextBox1.Text = str
  106. end
  107.  
  108. local function change_visible(mode)
  109.     mode = mode or false
  110.     BillboardGui0.Enabled = mode
  111. end
  112.  
  113. local morse_s = head:FindFirstChild('morse_s') or Instance.new("Sound")
  114. morse_s.Name = 'morse_s'
  115. morse_s.Volume = 1
  116. morse_s.Parent = head
  117.  
  118. local types = {
  119.     [0] = {
  120.         str = '.',
  121.         sound = 967014073,
  122.         speed = 0.16,
  123.     },
  124.     [1] = {
  125.         str = '-',
  126.         sound = 967014213,
  127.         speed = 0.31,
  128.     },
  129. }
  130.  
  131. local msg_id = 0
  132. local speed_multi = 2
  133.  
  134. local function play_morse(data)
  135.     msg_id += 1
  136.     local cur = msg_id
  137.     local str = ''
  138.     for _,beeps in pairs(data) do
  139.         for _,beep in pairs(beeps) do
  140.             if msg_id == cur then else return end
  141.             local bt = types[beep]
  142.             str = str .. bt.str
  143.             change_text(str)
  144.             local s = morse_s:Clone()
  145.             s.PlayOnRemove = true
  146.             s.SoundId = 'rbxassetid://' .. bt.sound
  147.             s.Volume = 2
  148.             s.Parent = morse_s.Parent
  149.             s:Destroy()
  150.             wait(bt.speed/speed_multi)
  151.         end
  152.         if msg_id == cur then else return end
  153.         str = str .. ' '
  154.         change_text(str)
  155.         wait(0.5/speed_multi)
  156.     end
  157. end
  158.  
  159. local function play_text(str)
  160.     local data = to_morse(str)
  161.     play_morse(data)
  162. end
  163.  
  164. game:GetService("RunService").Stepped:Connect(function()
  165.     if os.clock()-last_change >= 3 then
  166.         change_visible(false)
  167.     else
  168.         change_visible(true)
  169.     end
  170. end)
  171.  
  172. plr.Chatted:Connect(play_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement