If this code is a server script, it will only run a single time when the game starts. If it's a local script, it will run every time the player respawns. If it's a server script inside the Player, it won't run at all. And since you have a for loop, it will go through every message, just in a random order. Is this indented, or do you want it to pick a single message until the player dies again?
There are some other problems. The step variable is completely unnecessary because it does absolutely nothing. A for loop will repeat everything inside of it; line 06 will never be read more than once.
There is no need to make a table of numbers. Simply put the messages directly in the table. We will still be able to access a value from the table with a number by using tablename[index].
Lines 12-23 are very inefficient, when you have repeating code look for ways to use a for loop.
Naming your table table is a very bad idea. Not only is it a poor choice for a variable name, the word "table" is a keyword. This means that on Roblox it will turn blue (even though it doesn't on this website.) Nobody wants a blue variable, but even worse than that, using it as a variable name overrides the keyword! This makes it impossible to use table functions like table.remove() or table.insert(). When the following code is run,
local table = {"hi","bye"}
table.remove(table,1)
I get the following output:
11:35:23.688 - Workspace.Script:2: attempt to call field 'remove' (a nil value)
11:35:23.688 - Stack Begin
11:35:23.689 - Script 'Workspace.Script', Line 2
11:35:23.689 - Stack End
See? Because we made a variable named "table", the global keyword "table" was overridden.
Also, you make your changes in StarterGui, which is not what the Player is seeing. StarterGui is simply a container to put GUI objects, which are then cloned into PlayerGui when a Player joins the game and each time he/she respawns. If you want to edit what the player sees, make your changes in PlayerGui. This is why I recommend a LocalScript inside the GUI object.
Here is my attempt to improve your code based on the things previously stated. I'm assuming this is inside a GUI that has a Text property:
local messages = {
"Hello!",
"How are you?",
"Hola!",
"Como esta usted?"
}
local randomValue = messages[math.random(1, #messages)]
for i = 1, randomValue:len() do
script.Parent.Text = randomValue:sub(1,i)
wait(0.1)
end
2 comments
How do I make it so it changes the text on a gui?
Put it in a GUI that has a Text property...