Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use math.random for GUI buttons?

Asked by 4 years ago
Edited 4 years 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.

01function onClicked()
02    script.Parent.Parent.OFF.TextTransparency = 1
03    script.Parent.Parent.CURRADIO.TextTransparency = 0
04    math.random(1,3)
05    if 1 == true then
06        workspace.DCF.Play.Disabled = false
07        script.Parent.Parent.CURRADIO.Text = "Double Cleff FM"
08    end
09    if 2 == true then
10        workspace.DCF.Play2.Disabled = false
11        script.Parent.Parent.CURRADIO.Text = "MSX"
12    end
13    if 3 == true then
14        workspace.DCF.Play3.Disabled = false
15        script.Parent.Parent.CURRADIO.Text = "Flashback FM"
16    end
17end
18script.Parent.MouseButton1Click:connect(onClicked)
1
Check out my answer, also i failed to mention but, please use :Connect() as :connect() is deprecated. WideSteal321 773 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years 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:

1local int = math.random(1,3)

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

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

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:

01local int = math.random(1,3)
02function onClicked()
03    script.Parent.Parent.OFF.TextTransparency = 1
04    script.Parent.Parent.CURRADIO.TextTransparency = 0
05    math.random(1,3)
06    if int == 1 then
07        workspace.DCF.Play.Disabled = false
08        script.Parent.Parent.CURRADIO.Text = "Double Cleff FM"
09    end
10    if int == 2 then
11        workspace.DCF.Play2.Disabled = false
12        script.Parent.Parent.CURRADIO.Text = "MSX"
13    end
14    if int == 3 then
15        workspace.DCF.Play3.Disabled = false
16        script.Parent.Parent.CURRADIO.Text = "Flashback FM"
17    end
18end
19script.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 2196 — 4y
Ad
Log in to vote
0
Answered by 4 years 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

1if script.Parent.Value = 1 then
2--code
3end
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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

01function onClicked()
02    local int = math.random(1, 3) -- i put inside so the randomizer can change everytime
03    local texttable = {"Double Cleff FM", "MSX", "Flashback FM"}
04    script.Parent.Parent.OFF.TextTransparency = 1
05    script.Parent.Parent.CURRADIO.TextTransparency = 0
06    if int == 1 then
07        workspace.DCF.Play.Disabled = false
08        script.Parent.Parent.CURRADIO.Text = texttable[int] -- would be texttable[1] and then be Double Cleff FM
09    else -- if the int is 2 or 3
10        workspace.DCF["Play" .. int].Disabled = false -- would be workspace.DCF["Play1] or Play2.
11        script.Parent.Parent.CURRADIO.Text = texttable[int] -- would be texttable[2 or 3]
12    end
13end
14script.Parent.MouseButton1Click:Connect(onClicked) -- connect is deprecated

Answer this question