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

Random Text chooser (from Table) Isn't working?? (I'm newbie scripter)

Asked by 6 years ago

This isn't working, could someone explain to me what I need to do? It would help me learn tables + randomizing.

local mS = game.StarterGui.ScreenGui.Frame.TextLabel
local textmS = {
    "Hello",
    "Hi",
    "Nice to meet you",
    "!!",
    "Running out of things to say"
}

while true do
    mS.Text = textmS.Random()
    wait(10)
end
0
I do not believe '.Random()' is a function.. Optikk 499 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I get what you're trying to do but you're doing it wrong.

I don't know if the function "Random()" exists, but I suggest you use math.random()

local mS = game.StarterGui.ScreenGui.Frame.TextLabel

local textmS = {
    "Hello",
    "Hi",
    "Nice to meet you",
    "!!",
    "Running out of things to say"
}


while wait(10) do
local WhichTablePartion = math.random(5) -- We use 5 because there are five values stored in the table
mS.Text = textmS[WhichTablePartion]
end

Tested and works.

1
You should use math.random(#textmS) so that it automatically adapts to any table size. mattscy 3725 — 6y
0
Thanks, Matt. Ashley_Phantom 372 — 6y
Ad
Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
6 years ago

Link please familarize yourself a little bit more before using these features

I noticed several problems starting with the first line that you are trying to change StarterGui, if you want to affect the player's screen it must be in the PlayerGui (and a localscript), secondly you aren't referencing the table at all with "Random"...if you would like to understand tables more then you can read this

Random is just used to select a random number, getting the actual "random text message" is just using the table index based off the number Random provided

local mS = script.Parent.TextLabel
local textmS = {
    "Hello",
    "Hi",
    "Nice to meet you",
    "!!",
    "Running out of things to say"
}

while true do
    local random = Random.new():NextInteger(1, #textmS)
    mS.Text = textmS[random]
    wait(10)
end

Answer this question