Hello, I have a text load in effect, but I’m trying to make it pause for a second whenever the next piece of text it’s loading in is a “.”. I’ve tried counting each character and whenever i reaches a period, making it wait for one second but that’s so inefficient.
---Text is just a long sentence
game.ReplicatedStorage.GuiLoadEvent.OnClientEvent:Connect(function(text)
local sound = Instance.new("Sound")
sound.Parent = game.Players.LocalPlayer
sound.SoundId = "rbxassetid://1595977254"
local Gui = script.CaptainChat
Gui.Parent = game.Players.LocalPlayer.PlayerGui
local Message = Gui.BackGround.ChatBox
for i = 0, #text do
sound:Play()
Message.Text = string.sub(text,1,i)
print(#text)
wait(0.0625)
end
wait(2)
Gui:Destroy()
end)
for i = 0, #text do
local newString = string.sub(text, 1, i)
sound:Play()
Message.Text = newString
wait(0.0625)
if string.sub(newString, -1) == "." then wait(0.0625) end
end
Put any amount of time in the wait, just keep in mind that it will add that time to the normal time instead of replacing it unless you put the normal time in an else statement.
If you’re looking for a simple and cheap way to do this, then you can simply find what’s at the current substring and go from there by using your i variable as both the beginning and end of the search.
for i = 0, #text do
sound:Play()
Message.Text = text:sub(1, i)
local currentCharacter = text:sub(i, i)
if currentCharacter == "." then
wait(0.2)
else
wait(0.0625)
end
end
If you want a second opinion though: I recommend changing how you iterate over your string by using gmatch. gmatch is a generator for a for loop and what it does is essentially run through each match of a specified pattern. So, therefore, we can append text and check what character is currently up - all in an iteration.
The pattern for any character is ., so we use this and then check… what is the current character?
The following code sample would replace your entire for loop.
local currentText = ""
for character in text:gmatch(".") do
sound:Play()
currentText = currentText .. character
Message.Text = currentText
if character == "." then
wait(0.2)
else
wait(0.0625)
end
end
The above code sample is a two-in-one killer and I think is the best way to go about this. You’re both appending text and checking what the character in the iteration is, rather than having to do something like sub your text a second time.
I didn’t think about using gmatch. This solution is actually likely to be more efficient than mine. You should consider using this instead. @Scrizonn
Just to clarify something I thought might be confusing in his explanation, though, is that doing :gmatch(".") is not looking for a period character, but is instead looking for the next character in general (any character). When using gmatch, you use string patterns, and in this case using a period in the pattern would return a match for all characters.
I thought this might be confusing because if you do not understand string patterns you may be tempted to place a normal string rather than a pattern (and is even more confusing when you consider that this post is about looking for period characters in a string), as this would not work as intended.