Hello, I am making a survival game and it has a crafting table. I found a way to make it function but I believe there could be an easier way, let's say I could make recipes within an array. Is this possible?
Current code [LocalScript]
01 | local sl 1 = script.Parent.Slot 1. Item |
02 | local sl 2 = script.Parent.Slot 2. Item |
03 | local sl 3 = script.Parent.Slot 3. Item |
04 | local sl 4 = script.Parent.Slot 4. Item |
05 | local result = script.Parent.ResultImage |
06 | local ingreds = { |
07 | "Sticks" , |
08 | 'Logs' , |
09 | 'Rocks' , |
10 |
11 | } |
12 |
13 | while wait() do |
14 | --CraftingTable |
15 | if sl 1. Value = = ingreds [ 1 ] and sl 2. Value = = ingreds [ 1 ] and sl 3. Value = = ingreds [ 1 ] and sl 4. Value = = ingreds [ 1 ] then |
Not sure if this is gonna work... but it could be made like that
01 | local slots = { script.Parent.Slot 1. Item.Value,script.Parent.Slot 2. Item.Value,script.Parent.Slot 3. Item.Value,script.Parent.Slot 4. Item.Value } |
02 | local recipes = { |
03 | [ "campfire" ] = { "sticks" , "sticks" , "sticks" , "sticks" } ; |
04 | [ "baseball bat" ] = { "rocks" , "logs" , "logs" } ; |
05 | } |
06 |
07 | for thingThatWillBeCrafted, ingredients in pairs (recipes) do |
08 | for i = 1 , #ingredients do |
09 | if slots [ i ] = = ingredients [ i ] then |
10 | done [ i ] = = true |
11 | end |
12 | end |
13 | if #done = = #ingredients then |
14 | print ( "crafted " .. thingThatWillBeCrafted) |
15 | else |
16 | print ( "nothing" ) |
17 | end |
18 | end |