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.
01 | function 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.Play 2. Disabled = false |
11 | script.Parent.Parent.CURRADIO.Text = "MSX" |
12 | end |
13 | if 3 = = true then |
14 | workspace.DCF.Play 3. Disabled = false |
15 | script.Parent.Parent.CURRADIO.Text = "Flashback FM" |
16 | end |
17 | end |
18 | script.Parent.MouseButton 1 Click:connect(onClicked) |
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:
1 | local int = math.random( 1 , 3 ) |
Now that we have the random value, we can use it. Example:
1 | if int = = 1 then |
2 | print ( "Int is 1" ) |
3 | 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:
01 | local int = math.random( 1 , 3 ) |
02 | function 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.Play 2. Disabled = false |
12 | script.Parent.Parent.CURRADIO.Text = "MSX" |
13 | end |
14 | if int = = 3 then |
15 | workspace.DCF.Play 3. Disabled = false |
16 | script.Parent.Parent.CURRADIO.Text = "Flashback FM" |
17 | end |
18 | end |
19 | script.Parent.MouseButton 1 Click:Connect(onClicked) |
Accept this if it worked/helped you achieve your final answer!
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
1 | if script.Parent.Value = 1 then |
2 | --code |
3 | end |
Yes, WideSteal's answer is correct, but it could easily be reduced to 14 lines.
01 | function 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 |
13 | end |
14 | script.Parent.MouseButton 1 Click:Connect(onClicked) -- connect is deprecated |