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

Any Recommendations on How to Shorten This Code?

I am trying to shorten my code by a lot. One variable (in this instance it's chocolate cake) is 75+ lines of code. Most of the lines are just if/then statements. It works fine, but is a nightmare trying to add more variables to the code.

I've tried importing the information into a dictionary. This did not help me because I need more than just numeral values for the variables. a quick example:

["chocolateCakeRecipe"] = {
["All-Purpose Flour"] = 2;
}

I need it to say 2 cups for it to function with the tools (the tools have a numerical value attached to them and this includes fractions + this will be better explained in the code part). I also tried playing around with a module, but I never got it to function the way this long line of code does.

Here is a part of my code. The code for all of the ingredients repeat which is why I am showing a portion of the code. Each item has about 4-10 ingredients and I have around 30 items, so it is quite hectic. I am using a local script in a GUI.

local chocolateCake = script.Parent.Parent.Parent["Step3-Cake"].Chocolate
local frame = script.Parent.Parent.Parent.Parent.Notification
local main = script.Parent.Parent.Parent.Parent.Main
local item1 = frame.Main.ScrollingFrame.Item1
local item2 = frame.Main.ScrollingFrame.Item2
button.MouseButton1Click:Connect(function()
if frame.Visible == false then
		main.Visible = false
		--find out if its cake--
	if chocolateCake.BackgroundColor3 == Color3.fromRGB(255, 151, 151) then
				frame.Visible = true
				if game.Players.LocalPlayer.Backpack:FindFirstChild("2 Cups of All-Purpose Flour") then
					item1.Text = "Used 2 Cups of All-Purpose Flour {Left with 0 Cups}"
					item1.BackgroundColor3 = Color3.fromRGB(255, 162, 162)
					item1.Visible = true
					game.Players.LocalPlayer.Backpack:FindFirstChild("2 Cups of All-Purpose Flour"):Destroy()
				else
					item1.Text = "2 Cups of All-Purpose Flour {Missing 2 Cups}"
					item1.BackgroundColor3 = Color3.fromRGB(255, 125, 125)
				item1.Visible = true
				if game.Players.LocalPlayer.Backpack:FindFirstChild("1 Cup of All-Purpose Flour") then
					item1.Text = "2 Cups of All-Purpose Flour (Missing 1 Cup)"
				end
			end
			if game.Players.LocalPlayer.Backpack:FindFirstChild("¾ Cup of Baking Cocoa") or game.Players.LocalPlayer.Backpack:FindFirstChild("1 Cup of Baking Cocoa") or game.Players.LocalPlayer.Backpack:FindFirstChild("2 Cups of Baking Cocoa") then
				item2.Text = "3/4 Cups Baking Cocoa"
				item2.BackgroundColor3 = Color3.fromRGB(255, 162, 162)
				item2.Visible = true
				game.Players.LocalPlayer.Backpack:FindFirstChild("3/4 Cup of Baking Cocoa").Destroy()
				if game.Players.LocalPlayer.Backpack:FindFirstChild("1 Cup of Baking Cocoa") then
					game.Players.LocalPlayer.Backpack:FindFirstChild("1 Cup of Baking Cocoa").Name = "1/4 Cup of Baking Cocoa"
					item2.Text = "Used 3/4 Cup of Baking Soda {Left with 1/4 Cup}"
				end
				if game.Players.LocalPlayer.Backpack:FindFirstChild("2 Cups of Baking Cocoa") then
					game.Players.LocalPlayer.Backpack:FindFirstChild("2 Cup of Baking Cocoa").Name = "1/4 Cup of Baking Cocoa"
					item2.Text = "Used 3/4 Cup of Baking Soda {Left with 1 1/4 Cups}"
				end
			else
				item2.Text = "3/4 Cups Baking Cocoa"
				item2.BackgroundColor3 = Color3.fromRGB(255, 125, 125)
				item2.Visible = true
			end
else
	frame.Visible = false
end
end)

Let me know if I can shorten it through some loop or shorter if/then statement.

1 answer

You can have nested dictionaries inside of dictionaries.

Example

local itemsData = {

	["item1"] = {
		["price"] = 10
		["description"] = "bdbdga"
	}

}

Like this you can simply call the item index and apply all of its parameters like

for itemName, itemValues in pairs(itemsData) do
	
	priceLabel.Text = itemValues.price
	descLabel.Text = itemValues.description
end

vasically instead of checking if the name == to something just make stuff directly get assigned based on the dictionary index

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