How would I make a math.random choose a random piece of text inside of a table?

How would I make a math.random choose a random piece of text inside of a table?

3 Likes

table[math.random(1,#table)] , picks random stuff from table

2 Likes

Would this work?

local Tips = {"Tip: When wall hopping, flick your screen", "Tip: Dont give up!", "Tip: For a lot of skills, make sure you align yourself with parts correctly!", ""}

local txt = script.Parent:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")

while true do
	task.wait(0.5)
	
	txt.Text = table[math.random(1, Tips)]
	task.wait(10)
	for i = 1,10 do
		task.wait(0.5)
		
		script.Parent:WaitForChild("SurfaceGui").PixelsPerStud = script.Parent:WaitForChild("SurfaceGui").PixelsPerStud + 10
		task.wait(3)
		
		txt.Text = table[math.random(1, Tips)]
		
		script.Parent:WaitForChild("SurfaceGui").PixelsPerStud = script.Parent:WaitForChild("SurfaceGui").PixelsPerStud - 10
	end
end

Make sure to rename table to Tips. And add a #Tips instead of just Tips

Like:

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

1 Like
local tableOfSayings = {"Hi, how is your day going?","I got a win in Arsenal!","I got a victory royal in Fortnite!"}

function GenerateRandomString()

return tableOfSayings[math.random(1,#tableOfSayings)]

end

print(GenerateRandomString()) -- there is your random string
9 Likes

Question: Why did you use return like that? what does it do and mean?

just that line of code, how does it work?

Basically using the function will act as the random string so if I call the function I get the string.

local randomString = GenerateRandomString() -- runs the code inside the function and gives us the random string

1 Like

math.random returns a number so what do you want to do instead is get the number and then use the number as index in the table to get the value
like this for example,

local t = {"hello", "hi", "cool"}

local randomn = math.random(1, #t)
local result = t[randomn] 
3 Likes

Question: Is their a way to extend the messages time depending on how long it is?

Yes.

Send the script and I can do that for you.

1 Like
local Tips = {"Tip: When wall hopping, flick your screen", "Tip: Dont give up!", "Tip: For a lot of skills, make sure you align your Character with parts correctly!", "Tip: Obby Badges are helpful, especially for mobile players!", "Tip: Make sure to read the Graph In the Lobby to further understand how this games obby system works!", "Reminder, If you complete a Tower, you can still Practice the old obby tricks by going to the course you completed! Or, you can go to the Practice Area for all sorts of stuffz!"}

local txt = script.Parent:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")

local function GenerateText()
	return Tips[math.random(1, #Tips)]
end

while true do
	task.wait(10)
	
	for i = 1,10 do
		task.wait(0.01)
		script.Parent:WaitForChild("SurfaceGui").PixelsPerStud = script.Parent:WaitForChild("SurfaceGui").PixelsPerStud + 10
	end
	
	txt.Visible = true
	
	task.wait(3)
		
	txt.Text = GenerateText()
	
	for i = 1,10 do
		task.wait(0.01)
		txt.Visible = true
		script.Parent:WaitForChild("SurfaceGui").PixelsPerStud = script.Parent:WaitForChild("SurfaceGui").PixelsPerStud - 10
	end
end

Which wait do you want to be slowed down?

The one with the 10 second cooldown, since their might be really long messages, I would want the long messages to last for a bigger amount of time considering that if a player wanted to read it, they could not because they have such a short time to do so.

local Tips = {"Tip: When wall hopping, flick your screen", "Tip: Dont give up!", "Tip: For a lot of skills, make sure you align your Character with parts correctly!", "Tip: Obby Badges are helpful, especially for mobile players!", "Tip: Make sure to read the Graph In the Lobby to further understand how this games obby system works!", "Reminder, If you complete a Tower, you can still Practice the old obby tricks by going to the course you completed! Or, you can go to the Practice Area for all sorts of stuffz!"}

local txt = script.Parent:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")

local function GenerateText()
	return Tips[math.random(1, #Tips)]
end

while true do
	for i = 1, 10 do
		task.wait(0.01)
		script.Parent:WaitForChild("SurfaceGui").PixelsPerStud = script.Parent:WaitForChild("SurfaceGui").PixelsPerStud + 10
	end
	txt.Visible = true
	task.wait(1)
	txt.Text = GenerateText()
	task.wait(10)
	for i = 1,10 do
		task.wait(0.01)
		txt.Visible = true
		script.Parent:WaitForChild("SurfaceGui").PixelsPerStud = script.Parent:WaitForChild("SurfaceGui").PixelsPerStud - 10
	end
end

You nearly had it, you were just missing the length operator (#) in math.random() both times.

2 Likes