As I was walking to ^pl^, I saw ^pe^. Then, they ^v^. They started to approach me, so I quickly ^v^. After I hid behind ^t^, I became more paranoid, so I went ahead and ^v^.
The code:
local roundModule = {}
local teams = game:GetService("Teams")
local templates = game:GetService("ReplicatedStorage").AlibiTemplate
local key = {
templates.Nouns.Place,
templates.Nouns.Person,
templates.Nouns.Thing,
templates.Verbs
}
roundModule.Start = function()
for _, team in pairs(teams:GetTeams()) do
local stories = templates.StoryTemplates:GetChildren()
local curAlibi = stories[math.random(1,#stories)].Value
local unordered = {}
local keywords = {}
for _, folder in pairs(key) do
local shortcut = folder:GetAttribute("Shortcut")
local children = folder:GetChildren()
print("yayzers",shortcut)
if string.find(curAlibi,shortcut) then
for repetition in string.gmatch(curAlibi,shortcut) do
local newString, substitutions = string.gsub(curAlibi,shortcut,children[math.random(1,#children)].Value)
curAlibi = newString
table.find(unordered,repetition)
end
end
end
print(curAlibi)
end
end
return roundModule
The conditional string.find(curAlibi,shortcut) statement doesn’t run and the string prints as it started as with no changes added to it.
Please help, and if you have any questions, don’t hesitate to ask me!
I would ask you for more details but I think I might’ve figured out what went wrong here.
I’m making two assumptions here:
curAlibi equals to the string "As I was walking to ^pl^, I saw ^pe^. Then, they ^v^. They started to approach me, so I quickly ^v^. After I hid behind ^t^, I became more paranoid, so I went ahead and ^v^."
shortcut equals to one of these strings: "^pl^", "^pe^", "^v^", "^t^"
TL;DR: Usage of caret ‘^’ magic character at the very start of every shortcut causes this issue due to the fact that string.find uses string patterns by default, so do string.gmatch and string.gsub.
More Beginner Friendly Explanation
In Lua and, by extension, Luau, there is a feature called string patterns, also known as regular expressions (RegEx) in other popular languages, which is designed to make complex pattern matching in text easier. By default, string.find, alongside with string.gmatch and string.gsub, tries to match the pattern in given string using this feature.
In Lua, having a caret ‘^’ character at the beginning of the pattern denotes that it will be matched at the beginning of the input text, which is the case of every single possible value of shortcut. For example, if you were to match "^was" in "As I was walking", it would return nil because the string does not begin with "was".
Possible Fixes:
1. Replace indicator characters with non-magic ones (easy to implement, just need to know all magic characters)
Simply put, caret character needs to be replaced with something else that wouldn’t be interpreted as a magic character. Any of these ‘^$()%.[]*+-?’ characters are out of the question. Vertical bar ‘|’ is a good candidate.
2. Escape the caret character (easy to do but it won’t look pretty)
Lua allows you to escape magic characters by adding percentage ‘%’ sign before the magic character. In this case, "^pl^", "^pe^", "^v^" and "^t^" would turn into "%^pl%^", "%^pe%^", "%^v%^" and "%^t%^" respectively.