Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pick a random question to show on a gui? [UNSOLVED]

Asked by 9 years ago

I need to make a random table where a random question is shown on a gui? Like if it picked number one it would choose the question "Hello!" and 2 "How are you?" any help? I have some code if this would help..? :

local table = {1, 2, 3, 4, 5, 6, 7, 8, 9}

local numTab = #table
local step = 0

for i = 1, (numTab - step) do
local randomnum = math.random(1, #table)
table.remove(table[i])
step = step + 1
wait()
if randomnum == 1 then
    game.StarterGui.ScreenGui.Frame.q.Text = "H"
    wait(0.1)
    game.StarterGui.ScreenGui.Frame.q.Text = "He"
    wait(0.1)
    game.StarterGui.ScreenGui.Frame.q.Text = "Hel"
    wait(0.1)
    game.StarterGui.ScreenGui.Frame.q.Text = "Hell"
    wait(0.1)
    game.StarterGui.ScreenGui.Frame.q.Text = "Hello"
    wait(0.1)
    game.StarterGui.ScreenGui.Frame.q.Text = "Hello!"
    wait(0.1)
end
if randomnum == 2 then
    game.StarterGui.ScreenGui.Frame.q.Text = ""
end
end

Is there anything wrong with this? And there is no output :L??? I really need help, It is the key to my game..?

1 answer

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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:

--Create the table
local messages = {
"Hello!",
"How are you?",
"Hola!",
"Como esta usted?"
}

--Get the random value
local randomValue = messages[math.random(1, #messages)]

--Change the text
for i = 1, randomValue:len() do 
--The len() method gets the LENgth of a string.  You can also use the # symbol, as in #randomValue.
    script.Parent.Text = randomValue:sub(1,i)
--The sub() method gets substrings; In this case we are getting the substring with the position between 1 and i.
    wait(0.1)
end
0
How do I make it so it changes the text on a gui? antlerer 33 — 9y
0
Put it in a GUI that has a Text property... Perci1 4988 — 9y
Ad

Answer this question