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

How to make it so you can only do action if you have 4 ingredients? [Answered]

Hi, I'm trying to make it so if you have all 4 ingredients, which I am calling Tea Tree Oil, Sandalwood Oil, Rose Oil and a Daisy, then you can make another oil called a Daisy Healing Oil. This code is inside an object I named the Mixing Bowl, and will only activate if you click on the bowl. And btw, the code on the top I don't think I really need but it references to the tools in ServerStorage. (I made it so when you click on the jars it puts it in your inventory) This is my code:

local ServerStorage = game:GetService("ServerStorage")
local healing = ServerStorage:WaitForChild("Daisy Healing Oil")
local tree = ServerStorage:WaitForChild("Tea Tree Oil")
local sandal = ServerStorage:WaitForChild("Sandalwood Oil")
local rose = ServerStorage:WaitForChild("Rose Oil")
local daisy = ServerStorage:WaitForChild("Daisy")
local clickDetector = script.Parent:WaitForChild("ClickDetector")

function onClick(player)
	local inventory = player.Backpack
	for i, v in pairs(inventory:GetChildren()) do
		if v:IsA("Tool") and v.Name == "Tea Tree Oil" then
			v:Destroy()
		end
		if v:IsA("Tool") and v.Name == "Sandalwood Oil" then
			v:Destroy()
		end
		if v:IsA("Tool") and v.Name == "Rose Oil" then
			v:Destroy()
		end
		if v:IsA("Tool") and v.Name == "Daisy" then
			v:Destroy()
		end
	end
	local clone = healing:Clone()
	clone.Parent = inventory
	print("Added to inventory!")
end

clickDetector.MouseClick:Connect(onClick)

Right now all it does is that if you have the 4 ingredients it removes them from your inventory and gives you the healing oil, but I want to make so you have to have all 4 ingredients or else it won't do anything, for example if you were making a recipe you can't make it if you don't have all the ingredients. Thanks.

1 comment

0

make a variable local craftable = 0 in the first line of the function then instead of v:Destroy() type craftable = craftable + 1 then at the end if craftable == 4 then destroy the 4 ingredients and give the desired item

1 answer

✓ Accepted answer

Try this:

  • it uses a table (findtable) and checks the items inside of the click
local ServerStorage = game:GetService("ServerStorage")
local healing = ServerStorage:WaitForChild("Daisy Healing Oil")
local tree = ServerStorage:WaitForChild("Tea Tree Oil")
local sandal = ServerStorage:WaitForChild("Sandalwood Oil")
local rose = ServerStorage:WaitForChild("Rose Oil")
local daisy = ServerStorage:WaitForChild("Daisy")
local clickDetector = script.Parent:WaitForChild("ClickDetector")
local findtable = {
"Tea Tree Oil",
"Sandalwood Oil",
"Rose Oil",
"Daisy"
}
-- table of items ^ / 

function onClick(player) 
if player.Backpack:FindFirstChild(findtable[1]) and player.Backpack:FindFirstChild(findtable[2]) and player.Backpack:FindFirstChild(findtable[3]) and player.Backpack:FindFirstChild(findtable[4]) then
    local inventory = player.Backpack
    for i, v in pairs(inventory:GetChildren()) do
        if v:IsA("Tool") and v.Name == "Tea Tree Oil" then
            v:Destroy()
        end
        if v:IsA("Tool") and v.Name == "Sandalwood Oil" then
            v:Destroy()
        end
        if v:IsA("Tool") and v.Name == "Rose Oil" then
            v:Destroy()
        end
        if v:IsA("Tool") and v.Name == "Daisy" then
            v:Destroy()
        end
    end
else
print('not enough items')
end
    local clone = healing:Clone()
    clone.Parent = inventory
    print("Added to inventory!")
end

clickDetector.MouseClick:Connect(onClick)

2 comments

1

It works, but I put lines 36-38 in-between lines 32-33, because before (with how it is above) it wouldn't give me the healing oil. Thanks

0

No problem, glad you figured that out though! My bad.

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