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
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)