Turning this chunk of ElseIf's into readable code

I’ve made multiple threads on this gosh darn problem I have. Optimization/Readability of code. I’ve gotten loads of helpful tips from people I’ve talked to, to scouring the DevForum and looking at posts having the same problem I do. Although, my knowledge of coding is lackluster at best and I guess my mind was high when I wrote this code that I physically cannot process.

‘What Kinda Script Is This’ Script:

for i, NPCs in pairs(npcFolder:GetChildren()) do
	if NPCs.Name == "Test" then
		local welcomeTexts = {
			"hey",
			"hello",
			"how's the trip here?",
		}
		
		local goodbyeTexts = {
			"see you later.",
			"bye",
			"take care mate.",
			"alright, see ya.",
		}
		
		NPCs.NPC_Prompt.ProximityPrompt.Triggered:Connect(function()	
			camCFrame = currentCam.CFrame
			NPCs.NPC_Prompt.ProximityPrompt.Enabled = false
			currentCam.CameraType = Enum.CameraType.Scriptable
			Humanoid.AutoRotate = false
			Humanoid.WalkSpeed = 0

			task.wait()
			Random_Dialog = math.random(1,3)
			npcChange(NPCs.Name, NPCs.NPC_Cam.CFrame)

			frame.Visible = true
			frameTweenIn:Play()
			
			task.wait(0.35)
			nameTweenIn:Play()
			typeWrite(text, welcomeTexts[math.random(#welcomeTexts)])
		end)
		
		text.Activated:Connect(function()
			NPC_Amount += 1
			if Random_Dialog == 1 then
				if NPC_Amount == 1 then
					typeWrite(text, "testing text1.")

				elseif NPC_Amount == 2 then
					typeWrite(text, "testing text2.")

				elseif NPC_Amount == 3 then
					typeWrite(text, "testing text3.")

				elseif NPC_Amount == 4 then
					typeWrite(text, goodbyeTexts[math.random(#goodbyeTexts)])
					task.wait(2)

					frameTweenOut:Play()
					nameTweenOut:Play()

					text.Text = ""
					name.Text = ""

					NPC_Amount = 1
					npcChange("", camCFrame)
					NPCs.NPC_Prompt.ProximityPrompt.Enabled = true
					currentCam.CameraType = Enum.CameraType.Custom

					camCFrame = nil
				end
				Random_Dialog = 0
			elseif Random_Dialog == 2 then
				if NPC_Amount == 1 then
					typeWrite(text, "testing text1.")

				elseif NPC_Amount == 2 then
					typeWrite(text, "testing text2.")

				elseif NPC_Amount == 3 then
					typeWrite(text, "testing text3.")

				elseif NPC_Amount == 4 then
					typeWrite(text, goodbyeTexts[math.random(#goodbyeTexts)])
					task.wait(2)

					frameTweenOut:Play()
					nameTweenOut:Play()

					text.Text = ""
					name.Text = ""

					NPC_Amount = 1
					npcChange("", camCFrame)
					NPCs.NPC_Prompt.ProximityPrompt.Enabled = true
					currentCam.CameraType = Enum.CameraType.Custom

					camCFrame = nil
				end
				Random_Dialog = 0
			elseif Random_Dialog == 3 then
				if NPC_Amount == 1 then
					typeWrite(text, "testing text1.")

				elseif NPC_Amount == 2 then
					typeWrite(text, "testing text2.")
				elseif NPC_Amount == 3 then
					typeWrite(text, "testing text3.")

				elseif NPC_Amount == 4 then
					typeWrite(text, goodbyeTexts[math.random(#goodbyeTexts)])
					task.wait(2)

					frameTweenOut:Play()
					nameTweenOut:Play()

					text.Text = ""
					name.Text = ""

					NPC_Amount = 1
					npcChange("", camCFrame)
					NPCs.NPC_Prompt.ProximityPrompt.Enabled = true
					currentCam.CameraType = Enum.CameraType.Custom

					camCFrame = nil
				end
				Random_Dialog = 0
			end
		end)
	end
end

It works, but I just… :smiling_face_with_tear:
I’ve started with tables and putting all possible dialogue in their respective tables. Though my brain cannot continue due to both of my braincells not having the capacity to write decent code oh my GOSH.

local wlcm_N_gdby_Texts = {
			welcomeTexts = {
				"hey",
				"sup.",
				"hello.",
				"how's the trip here?",
			},
			
			goodbyeTexts = {
				"see you later.",
				"bye.",
				"take care.",
				"alright, see ya.",
			}
		}
		
		local dialogPaths = {
			Path1 = {
				[1] = "blah blah",
				[2] = "blah blah",
				[3] = "blah blah"
			},
			Path2 = {
				[1] = "blah blah",				
				[2] = "blah blah",
				[3] = "blah blah"
			},
			Path3 = {
				[1] = "blah blah",
				[2] = "blah blah",
				[3] = "blah blah"
			},
			Path4 = {
				[1] = "blah blah",
				[2] = "blah blah",
				[3] = "blah blah"
			},
		}

please help me i’ve been stuck with this problem for over 3 hours now

1 Like

I would rewrite the code into something more readable from scratch if you’re struggling that much.
Remember that the devfourm is not for asking people to write code for you.

A thing I really like doing in lua is putting functions inside tables

What you are then able to do is something like this

local Functions = {
	function(a,b) 
		-- Do stuff
	end,
	function(a,b) 
		-- Do stuff
	end,
	function(a,b) 
		-- Do stuff
	end,
	function(a,b) 
		-- Do stuff
	end,
}

Functions[NPC_Amount]() -- Run the function with index of NPC_Amount

If the code for the different indexes is the same, then you can store (the text in your situation) inside a table and use a single function. Otherwise you can use metatables to make a “main function” that run if a “explicit” function doesn’t exist, though that is more advanced


(if the function inside EasingFunctionTable doesn’t exist, then a general function that uses TweenSerivce is used, with the __index metamethod)

Since those use tables, you can also easily put it inside a module script if you have a lot of them

2 Likes

I know that, all I’m asking is a little help on what I can do to continue making with what I’ve remade already.

Thanks for the idea mate, completely forgot bout that :sweat_smile:

I misread what you said in your post, I apologize.

1 Like