Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why did this script break suddenly?

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)

1 comment

1

Please include the output if there is any.

1 answer

✓ Accepted answer

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 xs 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 ifs and elseifs 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?

12 comments

0

Holy $H!T!!!! Thank you so much! Luckily it isn't a TextBox, I have some buttons users can select that change the text.

0

THANK YOU THANK YOU THANK YOU THANK YOU!!! Seriously I cannot repeat Thank you enough times! My script is practically complete now- I believe I can do the rest going off what you did! I am luck jumping up and down with joy! Thank you!!!!!!!!!!

0

Quickly, Do I still need all my variables at the top for all the games? Like SBS, AR, etc.

1

Yea, of course, still define all the variables you use. That part doesn't need to be changed at all, so I didn't include them in the snippet on the answer.

0

I tested it and it doesn't work. Loading never becomes visible. I added all the variables right before your dictionaries, do you know of anything that could be wrong with it? I'm really sorry, I'm a very novice scripter, I need lots of help :)

0

Read the outpu! What does it say? What does it do instead?

0

And I can't show the output because my Studio crashes when I click Play Solo btw.

0

That doesn't have much to do with this script, does it? Can you isolate the problem? We can't work with no information; as far as I can tell, this script (on its own) shouldn't have any problems.

0

Here's my Output: 20:07:13.431 - wait(1) player = game.Players.LocalPlayer gui = player.Play:3: attempt to index global 'player' (a nil value) 20:07:13.442 - Stack Begin 20:07:13.444 - Script 'wait(1) player = game.Players.LocalPlayer gui = player.Play', Line 3 20:07:13.446 - Stack End

0

In order to use LocalPlayer, you have to be using a LocalScript, not a regular Script.

0

This is a LocalScript.

0

Should this be a Script instead?

This discussion is preserved as it was. New answers, comments, and votes are closed.