Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I implement string.sub into my script?

local ClickDetector = script.Parent.ClickDetector     --Makes the GUIs visible, then does the text.
ClickDetector.MouseClick:connect(function(User)
local player = User    
local dialogue = player.PlayerGui.ScreenGui1.DialogueBox
local close = player.PlayerGui.ScreenGui1.CloseButton
local name = player.PlayerGui.ScreenGui1.NameBox
local option1 = player.PlayerGui.ScreenGui1.Option1
local option2 = player.PlayerGui.ScreenGui1.Option2
name.Visible = true
option1.Visible = true
option2.Visible = true
close.Visible = true
dialogue.Visible = true
name.Text = "Cassie"
option1.Text = "Sure!"
option2.Text = "No, thanks."
dialogue.Text = string.sub("Hello.", 1, 6)
end)

This is everything I have so far. It is for my rpg game where I am making my own dialogue gui. I want the text to appear letter by letter, and I think that in order to make that work, I need to use string.sub for maximum efficiency. My problem is, I don't know how to efficiently change the value of the second number '6' or any other amount by adding 1. Of course, it'd start as only having 1, 1 then I need it to add 1 to the second number until everything in the string is on screen, then it'd stop until activated again when an option is pressed (which is in a different script) So, please help?

2 comments

0

For loop.

0

I know that, but I don't actually know how to do it correctly.

2 answers

✓ Accepted answer

If you need it to start at 1 and count up, you should use a for loop.

local ClickDetector = script.Parent.ClickDetector     --Makes the GUIs visible, then does the text.
ClickDetector.MouseClick:connect(function(User)
	local function say(string, text, w) -- Here's a function you could use, where 'string' is what you want to display, 'text' is the TextObject you want it to display in, and 'w' is the time you want it to wait between each letter.
		local waitTime = w or .2
		for i = 1, string.len(string) do
			text.Text = string.sub(string, 1, i)
			wait(waitTime)
		end
	end

	local player = User    
	local dialogue = player.PlayerGui.ScreenGui1.DialogueBox
	local close = player.PlayerGui.ScreenGui1.CloseButton
	local name = player.PlayerGui.ScreenGui1.NameBox
	local option1 = player.PlayerGui.ScreenGui1.Option1
	local option2 = player.PlayerGui.ScreenGui1.Option2
	name.Visible = true
	option1.Visible = true
	option2.Visible = true
	close.Visible = true
	dialogue.Visible = true
	name.Text = "Cassie"
	option1.Text = "Sure!"
	option2.Text = "No, thanks."
	say('Hello.', dialogue, .2) -- Example
end)

Hope this helped.

2 comments

0

Thanks a lot! It works and I'm going to study it a bit so I'll remember next time.

0

No problem.

So to have text be "typed out" as you request, you'd need to show the text you want in different phases, from when the first character is typed, to the last character. In order to do this, you will need to find the total amount of characters using:

string.len()

Now once you have the amount of text, you'd want to run a loop to type out the text, piece by piece. This is where string.sub() comes into play, it is used to read text at a designated starting point to a designated ending point. For example:

print(string.sub("Hi my name is Paul", 1, 5))

Would print - "Hi my", starting with the 1st character of H and ending with the 5th character of y. (This includes the spaces between texts)

The for loop uses both, string.sub() and string.len(). Now to bring this all together, you'd make something along the lines of...

local Message = ("Hello this is some kind of message I wish to display")
local TextBox = script.Parent:WaitForChild("TextBox") --- Let's just say this is inside a frame

for i=1, string.len(Message) do -- Remember, string.len() gets the amount of characters in the string!
	TextBox.Text = string.sub(Message,1,i)
	wait()
end --- The most important thing, if there isn't a wait, then the for loop runs instantaneously, so the visual effect doesn't happen. 

And ta-da, you've got yourself a typed out message. Now it's up to you to implement this into your own script. If you've got any other questions, feel free to hit me up.

1 comment

0

Thanks for this! I'll keep this in mind while I'm working on it!

This discussion is preserved as it was. New answers, comments, and votes are closed.