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

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

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

1 comment

0

I do not believe '.Random()' is a function..

2 answers

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.

2 comments

1

You should use math.random(#textmS) so that it automatically adapts to any table size.

0

Thanks, Matt.

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

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