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

Function doesn't execute?

function playstr(msg, text)
	for i = 1, string.len(msg) do
		text = string.sub(msg, 1, i)
		wait()
	end
end

playstr("Hi!", ScreenGui.Frame.TextLabel.Text)

This function is supposed to make a message play one character at a time when called, like so:

H
Hi
Hi!

However, when the function is called, the script seems to skip over the function entirely. For example, the script I have plays a message, waits for 2 seconds, then disappears. The script doesn't change any text, but still waits 2 seconds and removes the message. There are no notes in output. What's wrong with it?

1 answer

When you pass ScreenGui.Frame.TextLabel.Text, it just gets the text at that time (which is probably blank). It doesn't "link" the text parameter to that property:

When you set the text parameter, you just change the text parameter. It doesn't remember that it came from a TextLabel.

In other words, you didn't tell it to change any property of any text label, so it didn't.


You have to explicitly change properties to modify objects:

function playstr(msg, label)
    for i = 1, string.len(msg) do
        label.Text = string.sub(msg, 1, i)
        wait()
    end
end

playstr("Hi!", ScreenGui.Frame.TextLabel)

There's also a nice syntax, msg:sub(1, i) which I think is often much more readable.

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