How would I loop through every letter in a string?

Hi. I am making a radio system for a game and I want to make it so the further the person who sent the message is from you, the more corrupted and unreadable their message is.

I can’t figure out how I would loop through every letter in the string. I got everything down but that. Any help is appreciated!

I believe you can use string.gmatch for this (String Patterns)

An example of this would be:

local randomString = "Example"

for character in randomString:gmatch(".") do
   print(character)
end

Here, we cycle through the string that you choose, the . returns the next character every time the loop runs

local nstring = ""
for i,v in pairs(string.split(msg,"")) do
 if math.round(math.random()*string.len(msg)) > i/1.5  then
  nstring = nstring .. tostring(math.floor(math.random()*100)) -- scatter text, msg becomes higher chance of readable further along
 else
   nstring = nstring .. v
 end
end

just put this method in a function with a string argument called msg