I’ve been getting into the nifty looks of dialogue using string patterns and formats lately and am currently creating a loading screen with “animated” text.
print("Starting Life! Now loading...")
local LoadingScreen = script.Parent
LoadingScreen.Visible = true
local count = 0
local start = tick()
local LoadingTag = script.Parent.LoadingTag
while tick() - start < 6 do
LoadingTag.Text = "Starting a new Life " .. string.rep(".",count)
count = (count + 1)
wait(0.3)
end
count = 0
print("Finished Loading")
LoadingScreen.Parent = nil
script.Disabled = true
Though this is more for the sake of organization and esthetic, how would I allow the string.rep line to only repeat a maximum of 3 entries? (So that on the 4th second of the load, there would only be one dot, and for the 5th second, 2 dots etc.)
Every 3 count “steps” pretty much. I remember there being a string pattern I witnessed a streaming developer a while back use to accomplish this and I want to become acquitted to that kind of practice if effective.
Since the question is about string patterns, here’s an alternative solution that actually uses them. (You should definitely be using colbert’s way, I’m just putting this here for the sake of completeness.)
Basically, this will put the period the number of times specified minus one. Then, if there’s any instance where there are three periods in a row, it will remove those periods. Since that gives us a range of 0-2 periods, we can just concatenate (the .. operator, adds two strings together) another period onto it, which serves the same purpose as the + 1 part of colbert and Blokav’s code.
I see! I used colbert’s solution and it works like a charm. I actually ended up using 4 periods but thanks to him I understand how to change that at any time.
Also, I think I do understand now based on the way you explained there, but before I’d be 100% certain -
I read that on the Roblox wiki (a string):gsub() doesn’t actually “send” or return anything, but you’d used gsub so that the print() itself can make a string with a “subtracted” reference (not a returned value) by gsub?
Finally I think I understand that the reason we add a “%” before the period is because not adding it wouldn’t work as it’s a special character itself? (Quoted from the wiki)
Yeah, % in the context of a string pattern means that it acts literally as a period rather than as any character. string.gsub() returns a new, altered version of the inputted string, it doesn’t actually mutate the original string like in some languages. For example, in this example:
local x = "something here"
local y = x:gsub(".", "a")
print(x .. ", " .. y) --> something here, aaaaaaaaaaaaaa
You can see that x is unchanged. With my example, if you wanted to change it to four, you could just change the "%.%.%." to "%.%.%.%.". At that point, though, you’re better off just doing a repeat of "%.". Keeping that in mind, here’s a much messier but more configurable example:
local count = 5
local periodMaximum = 4
-- v this should print "Loading."
print("Loading." .. ("."):rep(count - 1):gsub(("%."):rep(periodMaximum),""))
This just implements the change where %. (the string pattern) is repeated a number of times equal to the maximum number of periods you want. As you can see, you’re definitely better off not using string patterns here. String patterns are super useful in other circumstances, but here it’s just a messier solution.
Very well! Thank you, for informing me about patters, posatta! I have some great things in mind as I look into this. Have a nice day!
(P.s. Off-topic [And I have to, I just couldn’t hold it back any longer but] what colbert said
While I was informed and now see that Modulo is much more than just a term after looking it up, I can’t help but picture a floating, default Roblox head wearing a monocle, top hat - kind of like this with text above it’s head that reads “Modulo” I’m weird