So I have a message script and I want to troll the player by changing their messages letters to random letters from a list
Use string.gsub:
string.gsub(“”, “”)
for example
string.gsub(“e”, “H”)
did you mean words? if so, then here’s how
local message = "i like meat"
local words = {"beat", "eat"}
local phrase = ""
math.randomseed(tick())
for i, v in next, message:split(" ") do
phrase ..= math.random()<0.5 and words[math.random(1, #words)] or v .. " "
end
-- there are 2^3 probabilities of different phrases.
no indent cause idk how
More details are needed for us to figure out what you want.
More specifically how does the randomization work? For example, are all letters replaced with random ones or only specific ones, if only specific ones how are the previous and specific letters picked? Are there more parameters about how letters should be replaced in regards to frequency, duplicates, and such or fully randomized? Are all the same letters in the message replaced with the same character or not? Should the output be filtered in case a swear word accidentally ends up on it? If so do you show tags or randomize it again until it’s filter-safe? Etc.
As you’ve suggested, the solution I am using for this approach involves creating a randomized list of characters from a string.
Such thing can be achieved by first utilizing the :split()
method of a string. Using simple terms, the string is split into parts with the certain characters provided and returned as a table.
For example:
local playerMessage = 'Hello, world!'
local splitMessage = playerMessage:split('') -- Here there are no characters given to the :split() method, because in '' there are no graphemes, thus it will completely chop the string into letters.
If you were to print()
the splitMessage you would understand what I mean.
Now that we have a table of the string we can use this solution I came up with to randomize the symbols in the table.
for letterIndex, letterValue in ipairs(splitMessage) do
table.remove(splitMessage, letterIndex)
table.insert(splitMessage, math.random(1, #splitMessage), letterValue)
end
Afterwards, if you want to change the playerMessage to this randomized table version of itself, you can do this:
playerMessage = ''
for letterIndex, letterValue in ipairs(splitMessage) do
playerMessage = playerMessage..letterValue
end