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

What did I do wrong while scripting a radio? I need help!

Asked by 5 years ago
Edited 5 years ago

So I'm making a radio system with "radio stations" in folders. I've made it to detect that a radio station is in game. The code is

local Frequency = script.Parent.Parent.Frequency
local Station1 = game.Workspace.RadioStations:FindFirstChild("Test1")
local Station2 = game.Workspace.RadioStations:FindFirstChild("Test2")
function onClicked()
if Frequency.Value == 1 then 
if Station1 then 
script.Parent.Parent.MessageScreen.SurfaceGui.SuccessMessage.Visible = true
elseif Frequency.Value == 2 then
elseif Station2 then 
script.Parent.Parent.MessageScreen.SurfaceGui.SuccessMessage.Visible = true


end
end
end 
script.Parent.ClickDetector.MouseClick:connect(onClicked)

The SurfaceGUI for Frequency 1 works, but if I set frequency to 2 and press the button, it doesn't show up the message. Please help me.

2 answers

Log in to vote
0
Answered by 5 years ago

So the code should be

local Frequency = script.Parent.Parent.Frequency
local Station1 = game.Workspace.RadioStations:FindFirstChild("Test1")
local Station2 = game.Workspace.RadioStations:FindFirstChild("Test2")
function onClicked()
    if Frequency.Value == 1 and Station1 then
        script.Parent.Parent.MessageScreen.SurfaceGui.SuccessMessage.Visible = true
    elseif Frequency.Value == 2 and Station2 then
        script.Parent.Parent.MessageScreen.SurfaceGui.SuccessMessage.Visible = true  
    end
end
16  script.Parent.ClickDetector.MouseClick:connect(onClicked)

The Prroblem

so you're using elseif to times in a row, you should use an and gate. Because elseif means if the previous elseif is false then do that one or skip it. so if you use an and gate it would work.

Tip Indent your code

0
Thank you! You've fixed my script! NewBuildmini 5 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Line 9: You should be using if instead of elseif.

You could also save yourself a couple lines by using and:

if (frequency.Value == 1) and (Station1) then
    -- blah blah
elseif (frequency.Value == 2) and (Station2) then
    -- blah blah 2
end
0
i already answered Luka_Gaming07 534 — 5y
0
i already answered Luka_Gaming07 534 — 5y
0
i already answered Luka_Gaming07 534 — 5y
0
ok theres this glitch going on Luka_Gaming07 534 — 5y
0
Beat me to it. LukeSmasher 619 — 5y

Answer this question