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

Can I get help with if statement logic on checking components?

I am in a situation where I am checking if a certain object contains certain components and does NOT contain other components.

To put it concretely, let's make ice cream.

There are 3 orders. Vanilla Sprinkles Gummies

"Sprinkles" and "Gummies" must have "Vanilla" as a component as well.

-- All their orders will be represented in strings
-- The problem I have is, how can I detect if the ice cream has vanilla and not the other ingredients? Also, how can I detect if the ice cream has vanilla and whatever was ordered as the topping? This is my if statement.

-- My main goal is to solve this problem on a scalable level, so if i add more ingredients I don't have to complicate my logic even further. I need help with constructing a sort of algorithm.

local ingredients = {"Vanilla", "Sprinkles", "Gummies"}
local iceCreamObj = obj -- doesn't matter. Just say it is a table
-- :Has() function is for simplicity.

local order = "Gummies"
function check()
	if iceCreamObj:Has(order) and iceCreamObj:Has("Vanilla") then
		return true
	end
	return false
end

check() -- true

-- See, this looks innocent, right? There is something missing here. What the ice cream has the order "Gummies" as well as "Sprinkles"? The if statement wouldn't detect that the sprinkles were there. How can I solve this? Should I make a table of ingredients excluding the vanilla and the ordered topping and loop through the ice cream's components to see if anything extra was added?

1 answer

✓ Accepted answer

This could be accomplished with a loop, if I'm understanding you right. Since you've got to have some sort of base component, Vanilla in this case, to be the ice cream (since gummies alone isn't ice cream), I would create a separate table for base ingredients, then a table for your toppings. I'm assuming you would have a physical aspect to this (whether it be decals on a sphere or whatever) to show their order in-hand. Let me know if you have any questions to my approach.

local ingredients = {"Vanilla", "Chocolate", "Strawberry"} -- flavors
local toppings = {"Sprinkles, Gummies"}

local function hasIngredient(obj)
	for _,v in pairs(ingredients) do
		if v == obj.ingredient then -- simplicity, edit of course
			return v -- or true. i return the ingredient name for future use
		end
	end
	return false -- doesnt have a base ingredient
end

local function isATopping(toppingName)
	for _,v in pairs(toppings) do
		if v == toppingName then
			return true
		end
	end
	return false
end

local function getToppings(obj)
	local tab = {}
	for _,objTopping in pairs(obj.toppings) do -- simplicity again. would use a :GetChildren() or whatever you need on the object
		if isATopping(objTopping) then
			table.insert(tab, objTopping)
		end
	end
	return tab
end
		
-- start your check here on an order			

if hasIngredient(iceCreamObj) then
	local tops = getToppings(obj) 
	if #topps > 0 then -- has toppings
		print'has toppings'
		for _,v in pairs(tops) do
			print('topping: '..v)
		end
	else
		print'no toppings :('
	end
else
	print'not a valid order!'
end
		

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