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.
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
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