Still have questions? Join our Discord server and get real time help.
0

# How to use math.random for GUI buttons?

Edited 9 days ago

Hi there. I'm trying to make a radio where you can listen to various different radio stations. The radio scripts are stored in the workspace, and I'm trying to make so you can switch radio stations. My scripts for the radio station don't work though.

function onClicked()
script.Parent.Parent.OFF.TextTransparency = 1
math.random(1,3)
if 1 == true then
workspace.DCF.Play.Disabled = false
end
if 2 == true then
workspace.DCF.Play2.Disabled = false
end
if 3 == true then
workspace.DCF.Play3.Disabled = false
end
end
script.Parent.MouseButton1Click:connect(onClicked)

1
Check out my answer, also i failed to mention but, please use :Connect() as :connect() is deprecated. WideSteal321 707 — 9d

0
Edited 9 days ago

math.random's value cannot be accessed like that. A good way of doing this is creating a variable called something like int or whatever you choose, like this:

local int = math.random(1,3)


Now that we have the random value, we can use it. Example:

if int == 1 then
print("Int is 1")
end


How you used it if 1 == true then will not work, as it is asking if the number three is true, not your value if it is 3. Your full code would be something along the lines of:

local int = math.random(1,3)
function onClicked()
script.Parent.Parent.OFF.TextTransparency = 1
math.random(1,3)
if int == 1 then
workspace.DCF.Play.Disabled = false
end
if int == 2 then
workspace.DCF.Play2.Disabled = false
end
if int == 3 then
workspace.DCF.Play3.Disabled = false
end
end
script.Parent.MouseButton1Click:Connect(onClicked)


0
Line 5 is useless, and I recommend you putting the int variable inside of the clicked function, so it randomizes every time you click. raid6n 2290 — 8d
0

WideSteal321's way is probably easier but my way is use values. Create 3 values and make the math.random change the value and to change it just go

if script.Parent.Value = 1 then
--code
end

0
raid6n 2290
8 days ago
Edited 8 days ago

Yes, WideSteal's answer is correct, but it could easily be reduced to 14 lines.

function onClicked()
local int = math.random(1, 3) -- i put inside so the randomizer can change everytime
local texttable = {"Double Cleff FM", "MSX", "Flashback FM"}
script.Parent.Parent.OFF.TextTransparency = 1