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

How to script a click function that changes a text, using type writer?


local noob = "Okay, here's our menu!"

function eh() for a = 5, #noob do script.Parent.Text = string.sub(noob, 2, a) wait(0.06) return eh() end end

script.Parent.MouseButton1Click:Connect(function(Clicked) eh() end)


This is the script i used, but its not working how it is supposed to, i'm a newbie in scripting

4 comments

0

the "noob" variable is the text.

0

Insert your code in a code block. To do that: Click on edit to edit this post. Click on the blue Lua icon which will insert 2 squiggly lines (~). Place your code in between these two lines.

0

or press the lua button next to the inline code button

0

Thanks everyone for the help!

2 answers

✓ Accepted answer

If you want to make a function that types one letter at a time, you just make a new function that makes a copy of the original string (so that it doesn't override it) and then you use the string:sub() method to make it extract a portion of the string and then you do this with a for loop.

Consider the following code:

local text = "Hello World";
print(text:sub(1,5)) --> Hello

That codes prints the 5 first characters of the string ("Hello").

Now, you just need to make a numeric for loop that goes through each character and updates the TextLabel:

function writeText(text, textLabel)
	for current = 0, #text, 1 do
		textLabel.Text = text:sub(1,current)
	end
end

writeText("Hello World", script.Parent.TextLabel)

There you go.

2 comments

0

Thank you!

0

Would you mind accepting the answer as an answer?

Hello, utrabem!

I made some changes on your script, first of all, :Connect(function() is a function, so you don't need to call another function, to get a string lenght, use string.len() and if you want the letters to change on the string, use str = str2!

local noob = "Okay, here's our menu!"

script.Parent.MouseButton1Click:Connect(function()
	for a=1, string.len(noob)+1 do 
		script.Parent.Text = noob
		noob = string.sub(noob, 2)
		wait(0.06)
	end 
end)

Attention, I didn't made any changes on what your script does, this makes the words be REMOVED like somone writing...

Helpful links:

Roblox Dev - Lua Libraries - Strings

Good Luck with your games

1 comment

0

Thank you :)

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