How to clean this up and fix it

I want this code to be cleaner and fixed

It is very messy and I do not know how to make my code cleaner to where it works :frowning: It also tries to index nil with “Mats” on my table

YouTube, DevForum

I am using a LocalScript and when I try to use the if statement for if the player has a certain amount of LOVE(level) and Gold, and materials so they can craft it. But when I try to do this, it will attempt to index nil with “Mats” which I don’t know why since there is “Mats” part in my table.

local MainFrame = script.Parent:WaitForChild("MainFrame")
local ItemInfo = MainFrame:WaitForChild("ItemInfo")
local materialNeeded,itemIcon,itemName = ItemInfo.MaterialButton,ItemInfo.ItemIcon,ItemInfo.ItemName
local CraftingFrame = MainFrame:WaitForChild("CraftableItems")
local player = game.Players.LocalPlayer

local mainTable = {
	["Plastic"] = {
		["Weapon"] = nil,
		["Armor"] = nil,
		["Mats"] = {
			["LOVE"] = {
				"LOVE",
				10,
			},
			["Currency"] = {
				"Gold",
				1000,
			},
			["GlassShard"] = 1,
			["Lava"] = 1,
			["Plastic"] = 0,
		};
	}
}

workspace:WaitForChild("CraftingStation"):WaitForChild("CraftPart").Touched:Connect(function(h)
	MainFrame.Visible = true
	
	local c = game.Lighting:WaitForChild("CraftingItems"):GetChildren()
	for i=1,#c do
		local button = Instance.new("TextButton")
		button.Parent = CraftingFrame
		button.Size = UDim2.new(0.8,0,0.1,0)
		button.Visible = true
		button.BackgroundColor3 = Color3.new(0,0,0)
		button.BorderColor3 = Color3.new(1,1,1)
		button.BorderSizePixel = 5
		button.Text = c[i].ItemName.Value
		button.TextColor3 = Color3.new(1,1,1)
		button.TextScaled = true
		button.Font = Enum.Font.Arcade
		
		button.MouseButton1Click:Connect(function()
			--this is where it errors
			if player:WaitForChild("Materials")["Plastic"].Value >= mainTable[button.Text]["Mats"]["Plastic"] and player:WaitForChild("Materials")["Lava"].Value >= mainTable[button.Text]["Mats"]["Lava"] and player:WaitForChild("Materials")["GlassShard"].Value >= mainTable[button.Text]["Mats"]["GlassShard"] and player:WaitForChild(mainTable[button.Text]["Mats"]["Currency"][1]).Value >= mainTable[button.Text]["Mats"]["Currency"][2] and player:WaitForChild(mainTable[button.Text]["Mats"]["LOVE"][1]).Value >= mainTable[button.Text]["Mats"]["LOVE"][2] then
				if mainTable[button.Text]["Weapon"] and player:WaitForChild("Weapons")[mainTable[button.Text]["Weapon"]] then 
					player:WaitForChild("Weapons")[mainTable[button.Text]["Weapon"]].Value = true
				end
				if mainTable[button.Text]["Armor"] and player:WaitForChild("Armor")[mainTable[button.Text]["Armor"]] then
					player:WaitForChild("Armor")[mainTable[button.Text]["Armor"]].Value = true
				end
				print("Crafted")
			else
				print("No")
			end
		end)
	end
end)
1 Like

By the way this is all in beta it is a W.I.P so do expect it to not work I have tried fixing this for a while now and I can’t

1 Like

Code is not really “messy” as long as you know what it is doing. I wouldn’t worry about the length or amount of code. I’d suggest going through it separating out some of the longer lines so that they can be easier to read. I’d suggest you start by checking if things exist before indexing them.

I’d set up variables for each time you index something so that you can check if they exist to prevent your code from erroring. It could also help figure out where it is doing something that you do not expect.

local materialName = button.Text
local materialTable = mainTable[materialName]
if materialTable then
	local matsTable = materialTable["Mats"]
	if matsTable then
		...
1 Like

This error happens when I have enough materials else it will work

1 Like