Outputting a random text with a table

(Sorry, I don’t know if this code is a table or not)

local texts = {
"text1",
"text2",
"text3"
	}

text.text = (texts[math.random(1, #texts)])
--script.parent.text is the text.text

How do I change (texts[math.random(1, #texts)]) into the text?

I want one of the 3 texts to be put as the text.

texts[math.random(1, #texts)]

should return a string, like

texts[2]

returns "text2".

Isn’t that was I exactly did?

Summary

This text will be hidden

Yes, so I don’t see the problem, is there an error message?

No, I dont know why this isn’t working. Want to see the whole script?

I guess it doesn’t hurt to see the script.

Also, is the text and textLabel visible?

local text = script.Parent
game.Players.PlayerAdded:Connect(function()
	local texts = {
		"text1",
"text2",
"text3"
	}
	
	text.text = (texts[math.random(1, #texts)])
end)

Yes.

If you want the workspace I can give you that, and a screenshot of what the text is in the game.

I assume this is a server script, not in the StarterGUI (Because you don’t use PlayerAdded in StarterGUI, nor do you typically do so in a LocalScript), so maybe make sure the script and TextLabel is under starterGUI, or get’s parented to the player’s gui.

Also, if you must use game.Players, try using local Players = game:GetService("Players").

Oops, my bad. I used player added in starterGUI. I removed PlayerAdded and stuff but it still doesn’t work.

try using a print statement like this:

local texts = {
	"text1",
	"text2",
	"text3"
}

local textLabel = script.Parent	

local text = texts[math.random(1, #texts)]
print(text)
textLabel.Text = text
1 Like

I tried that, it still didn’t work.

What did it print in the console?

it printed one of the texts, but it also printed “Text is not a valid member of Text label”

Edit: you probably forgot to capitalize .text into .Text
Edit 2: it works now, you just forgot to capitalize

OH, the property is Text, not text, I should have realized that earlier lol.

1 Like

Thanks for helping! now I can refer to this whenever I have the same problem.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.