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?
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.