I have a crafter/fuser in my game. Players can then put three objects on three pads like so:
Then, they press a button or whatnot to start it. The three objects are then put into a table using GetPartsInPart() from that semi-transparent red block.
I then make sure the parts are not character parts, etc…
I have stored recipes like so:
local DiHydrogen = {"Hydrogen", "Hydrogen"}
local Water1 = {"Hydrogen", "Hydrogen", "Oxygen"}
local Water2 = {"DiHydrogen", "Oxygen"}
I then use the following formula to check if the parts in the red block are equal to the recipes above
local function AreTablesEqual(t1, t2)
if #t1 ~= #t2 then
--warn("Wasn't equal, returning false.")
return false
end
table.sort(t1)
table.sort(t2)
for i, v in ipairs(t1) do
if v ~= t2[i] then
--warn("if "..v.." is not equal to "..t2[i])
--warn("v ~= t2[i], returning false.")
return false
end
end
--warn("No problems detected. Returning true.")
return true
end
This ensures that we make sure the tables are equal. The problem is this entire block of code:
if AreTablesEqual(FusingPartsTable, DiHydrogen) then
warn("FusingPartsTable is equal to the Recipe for DiHydrogen!")
-- Remove the FusingParts
for i,v in pairs(InstanceFusingPartsTable) do
v:Destroy()
end
-- Spawn DiHydrogen
local Clone = Molecules:WaitForChild("DiHydrogen"):Clone()
Clone.Parent = ElementsPlaced
Clone.Position = Output1Part.Position + Vector3.new(0, ydisplacement, 0)
if Clone:FindFirstChild("Owner") then
Clone:FindFirstChild("Owner").Value = player.Name
end
end
(This makes sure that if they are equal, it deletes the fusing parts, and spawns the fused item and puts it on the black pad in the first image.)
I have to copy paste this for every recipe that I add, and change all of the values. How the heck do I make this more simple?
Edit: FusingPartsTable stores the names of the parts, while InstanceFusingPartsTable stores the ObjectValue of each part, so that I can destroy them when the fusing is done.