I need help with a TIPs GUI

Hey, I wanted to make a simple GUI that displays tips stored inside a table. I don’t understand why this doesn’t work, I’ve probably done something wrong somewhere in the script…

Here is a ss of the script, the script type is a LocalScript stored inside a TextLabel.
image

I figured just doing math.random(advice) would select a random item from the table, but I’m potentially wrong?

Since this is a dictionary, math.random won’t work like you’re trying to make it.
A simple solution would be to convert it into an array.

Something like -

local advice = {
 "Need help? ...",
"The Vice Chief.."
}

Also, math.random requires 2 arguments.
do -

math.random(1,#advice)

Tip:
Use task.wait instead of wait.

Still using math.random?
image

Do:

txt.Text = advice[math.random(1,#advice)]

Please notice to my edit -

txt.Text = advice[math.random(1,#advice)]

Ill try this.

What does the # do?

It stands for the amount of items your table has.
Example:

#game.Workspace:GetChildren() would return me the amount of children there are currently in workspace.

Hey there, math.random() is a function used to generate a random number between 2 numbers. You would only need to put in 2 parameters. The first parameter, is the lowest number it would choose. The second number would be the highest number it would choose. For example, if you put in math.random(1, 5), it would choose either 1, 2, 3, 4, or 5.

For this code try this:

txt.Text = advice[math.random(1, #advice)]

Try it out and let me know if this works.

Someone already told me this but thx

2 Likes

Cool. I still cant get it to work though… When i play the game it doesnt do anything
image

image

Try this

local advice = {
"Advice1";
"Advice2";
}

local timeDiff = 15
local txt = script.Parent

while task.wait(timeDiff) do
    txt.Text = advice[math.random(1,#advice)]
end

Where are you calling the function?
You need to call it if you want it to run

If you want to run it without a function,
this would work -

local advice = {
"Advice1";
"Advice2";
}

local timeDiff = 15
local txt = script.Parent

while task.wait(timeDiff) do
    txt.Text = advice[math.random(1,#advice)]
end

You’re not calling the “tip()” function

1 Like

Ill try this, but seriously WOW, this is complicated for me. LOL

Update: It works

1 Like
local advice = {
	"1",
	"2",
	"3",
	"4",
	"5"
}

local timeDiff = 15
local txt = script.Parent

local function tip()
	while true do
		txt.Text = advice[math.random(1,#advice)]
		task.wait(timeDiff)
	end
end


game.Players.PlayerAdded:Connect(tip)

Glad to hear it! Make sure to mark my response as the solution, so people know it’s solved :slight_smile:

It is not too complicated, if you want - I could explain you each step in the dms.

BAH! That is quite embarrising, I had it working in another way tho! Thanks for responding so fast though!

1 Like

:slight_smile:

character limit stuff lol hoi