So this script is supposed to look at the text of 2 GUIs, and then make certain GUIs visible based on what they say. I added the Medium option for Building and Comedy, and suddenly the entire script won't work. Can you please help me? Here is my script, sorry it is a little long... Also all my hierachies are correct.
wait(1) player = game.Players.LocalPlayer gui = player.PlayerGui.ScreenGui results = gui.results stepone = results.step1choice steptwo = results.step2choice TMM = results.TMM LD = results.LD AFSS = results.AFSS AR = results.AR NDS = results.NDS TQ = results.TQ SBS = results.SBS S = results.S WTRB = results.WTRB PIR = results.PIR HITW = results.HITW DI = results.DI BB2 = results.BB2 PTP = results.PTP D2 = results.D2 Loading = results.Loading results.Changed:connect(function() local value = results.Visible wait(1) Loading.Visible = true wait(3) if stepone.Text== "Action & Adventure" then if steptwo.Text== "Easy" then local num = math.random(1,2) if num== 1 then TMM.Visible = true Loading.Visible = false elseif num==2 then LD.Visible = true Loading.Visible = false end elseif steptwo.Text== ("Medium") then local num = math.random(1,4) if num==1 then AR.Visible = true Loading.Visible = false elseif num==2 then AFSS.Visible = true Loading.Visible = false elseif num==3 then NDS.Visible = true Loading.Visible = false elseif num==4 then TQ.Visible = true Loading.Visible = false end end elseif stepone.Text == ("Building & Comedy") then if steptwo.Text == ("Easy") then local num = math.random(1,7) if num== 1 then SBS.Visible = true Loading.Visible = false elseif num==2 then S.Visible = true Loading.Visible = false elseif num==3 then WTRB.Visible = true Loading.Visible = false elseif num==4 then PIR.Visible = true Loading.Visible = false elseif num==5 then HITW.Visible = true Loading.Visible = false elseif num==6 then DI.Visible = true Loading.Visible = false elseif num==7 then BB2.Visible = true Loading.Visible = false end elseif steptwo.Text == ("Medium") then local num=math.random(1,2) if num == 1 then PTP.Visible = true Loading.Visible = false elseif num==2 then D2.Visible = true Loading.Visible = false end end end end)
First: tab your code correctly. There are a few lines it's off. You should also keep space around your operators; a == b
is a lot easier to read than a==b
, the same goes for a = b
. There's also no reason to put parenthesis around your strings..
Now look at how many times Loading.Visible = false
happens. It is written 15 times, even though it can just go once at the end.
Now notice that you continually say x.Visible = true
for different x
. The x
only depends on what num
is, which is 1, 2, 3, 4, ...
. This suggests we should probably be getting these x
s from lists.
results.Changed:connect(function() local value = results.Visible wait(1) Loading.Visible = true wait(3) if stepone.Text == "Action & Adventure" then if steptwo.Text == "Easy" then local options = {TMM, LD} local num = math.random(#options) options[num].Visible = true elseif steptwo.Text == "Medium" then local options = {AR, AFSS, NDS, TQ} local num = math.random(#options) options[num].Visible = true end elseif stepone.Text == "Building & Comedy" then if steptwo.Text == "Easy" then local options = {SBS, S, WTRB, PIR, HITW, DI, BB2} local num = math.random(1, #options) options[num].Visible = true elseif steptwo.Text == "Medium" then local options = {PTP, D2} options[num].Visible = true end end Loading.Visible = false end)
Now we are repeating ourselves again:
local num = math.random(#options) options[num].Visible = true
The thing that is different here is options
, so we just need to select that.
results.Changed:connect(function() local value = results.Visible wait(1) Loading.Visible = true wait(3) local options -- Declare options if stepone.Text == "Action & Adventure" then if steptwo.Text == "Easy" then options = {TMM, LD} elseif steptwo.Text == "Medium" then options = {AR, AFSS, NDS, TQ} end elseif stepone.Text == "Building & Comedy" then if steptwo.Text == "Easy") then options = {SBS, S, WTRB, PIR, HITW, DI, BB2} elseif steptwo.Text == "Medium" then options = {PTP, D2} end end local num = math.random(#options) options[num].Visible = true Loading.Visible = false end)
Actually, we can eliminate the if
s and elseif
s altogether. We can just make a dictionary with two levels:
local data = {} local action = {} action.Easy = {TMM, LD} action.Medium = {AR, AFSS, NDS, TQ} data["Action & Adventure"] = action local building = {} building.Easy = {SBS, S, WTRB, PIR, HITW, DI, BB2} building.Medium = {PTP, D2} data["Building & Comedy"] = building results.Changed:connect(function() local value = results.Visible wait(1) Loading.Visible = true wait(3) local options = data[stepone.Text][steptwo.Text] local num = math.random(#options) options[num].Visible = true Loading.Visible = false end) -- 79 lines have become 22: cut out more than 2/3 of it -- leaving much less that you have to be able to understand
Though I am now assuming that stepone.Text
actually is a valid option. If it's a TextBox, that might not be true (they could mistype it). Though, expecting the player to accurately type "Building & Comedy" probably isn't a good choice.
What does the output say?