Hey Devs. I want to make a frame that shows a string out of a handful of strings and every time you press a button it randomizes what string shows. Anyone know how to do this? If so, that would be much appreciated. Thanks!
localscript inside of a Text button
local btn = script.Parent
local messages = {“Wow”,“sus”,“amogus”} – add new messages to this table
btn.MouseButton1Click:Connect(function()
btn.Text = messages[math.random(1,#messages)]
end)
It didn’t work for some reason…
I put that in a local script inside a textbutton.
Did you get any errors in the output? The code Denis sent seems like it should work.
No errors.
Denis’s code doesn’t have certain part. Majority of issue is lack of randomseed.
math.randomseed(tick())
local btn = script.Parent
local messages = {"Wow", "sus","amogus"} – add new messages to this table
btn.MouseButton1Click:Connect(function()
btn.Text = messages[math.random(#messages)]
end)
Edit: Fixed majority of syntax issue.
Could you show a video or image of the game, showing it not working? Because again, the code looks fine.
What does this mean?
Oh. I see, you just have a syntax error. Remove the “- add new messages to this table” thing.
Well, uh, that’s weird. No idea why it’s still giving you a syntax error. Try this code instead:
local button = script.Parent
local messages = {"Be motivated!", "You did well today!", "Good work!"}
button.MouseButton1Click:Connect(function()
button.Text = messages[math.random(1, #messages)]
end)
replace “
and ”
inside a table with "
Oh yea, that explains the error. The quotes aren’t the right type or something-
It worked thanks. Now, I want to make it when you press the button that text shows in the middle of the frame. So it always shows new on the button but different text every time in the middle.
Alright, so instead of setting the button text, you want to set the label on the gui’s text. Could you show a picture of what’s inside the GUI in the explorer?
Alright, change the code to this:
local button = script.Parent
local messages = {"Be motivated!", "You did well today!", "Good work!"}
local UI = button.Parent -- Frame
local TextLabel = UI.TextLabel
button.MouseButton1Click:Connect(function()
TextLabel.Text = messages[math.random(1, #messages)]
end)
i guess TextLabel is your gui title?
local Button = script.Parent
local Label = Instance.new("TextLabel")
Label.AnchorPoint = Vector2.new(0.5, 0.5)
Label.Position = UDim2.new(0.5, 0, 0.5, 0)
Label.TextScaled = true
Label.Size = UDim2.new(0.5, 0, 0.5, 0) -- change to yours
Label.Parent = Button.Parent
local messages = {
"1",
"2",
"3",
"4",
}
Button.MouseButton1Down:Connect(function()
math.randomseed(tick())
Label.Text = messages[math.random(1, #messages)]
end)
Thanks so much!