Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to use math.random for GUI buttons?

Asked by 9 days ago
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
    script.Parent.Parent.CURRADIO.TextTransparency = 0
    math.random(1,3)
    if 1 == true then
        workspace.DCF.Play.Disabled = false
        script.Parent.Parent.CURRADIO.Text = "Double Cleff FM"
    end
    if 2 == true then
        workspace.DCF.Play2.Disabled = false
        script.Parent.Parent.CURRADIO.Text = "MSX"
    end
    if 3 == true then
        workspace.DCF.Play3.Disabled = false
        script.Parent.Parent.CURRADIO.Text = "Flashback FM"
    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

3 answers

Log in to vote
0
Answered by 9 days ago
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
    script.Parent.Parent.CURRADIO.TextTransparency = 0
    math.random(1,3)
    if int == 1 then
        workspace.DCF.Play.Disabled = false
        script.Parent.Parent.CURRADIO.Text = "Double Cleff FM"
    end
    if int == 2 then
        workspace.DCF.Play2.Disabled = false
        script.Parent.Parent.CURRADIO.Text = "MSX"
    end
    if int == 3 then
        workspace.DCF.Play3.Disabled = false
        script.Parent.Parent.CURRADIO.Text = "Flashback FM"
    end
end
script.Parent.MouseButton1Click:Connect(onClicked)

Accept this if it worked/helped you achieve your final answer!

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
Ad
Log in to vote
0
Answered by 9 days ago

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
Log in to vote
0
Answered by
raid6n 2290 Moderation Voter Community Moderator
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
    script.Parent.Parent.CURRADIO.TextTransparency = 0
    if int == 1 then
        workspace.DCF.Play.Disabled = false
        script.Parent.Parent.CURRADIO.Text = texttable[int] -- would be texttable[1] and then be Double Cleff FM
    else -- if the int is 2 or 3
        workspace.DCF["Play" .. int].Disabled = false -- would be workspace.DCF["Play1] or Play2.
        script.Parent.Parent.CURRADIO.Text = texttable[int] -- would be texttable[2 or 3]
    end
end
script.Parent.MouseButton1Click:Connect(onClicked) -- connect is deprecated

Answer this question