Attribute not executing code when changed

Im making a cooking game, and the whole idea is that the food items all have a cooked attribute that changes depends on the temperature of the food, It gets cooked when it is below the MaxTemp and Above the MinTemp for cooking. But for some reason whenever i set the CurrentTemp attribute to a different temp the AttributeChangedSignal function does not fire.

Heres the code:

local collectionservice = game.CollectionService

collectionservice:GetInstanceAddedSignal("food"):Connect(function(instance:Part)
	instance:GetAttributeChangedSignal("CurrentTemp"):Connect(function()
		if instance:GetAttribute("CurrentTemp") <= instance:GetAttribute("MaxTemp") and instance:GetAttribute("CurrentTemp") >= instance:GetAttribute("MinTemp") then
			instance:SetAttribute("cooked", true)
		end
	end)
end)

Help would be appreciated

Make sure that an attribute is set for the instance… Or in setting another attribute value by using SetAttribute(), else, try checking if the GetInstanceAddedSignal gets fired or printed at least, then debug your way out of there.

The instance you’re refering to probably already exists ingame and therefore the GetInstanceAddedSignal never fires resulting to GetAttributeChangedSignal not getting binded

it seems to detect when the food is added to the game and nothing else

If the food already exist in the game it won’t fire. This might cause the bug for already existing food

i clone the food from serverstorage and im trying to change the attributes on the clone, the script does know when the food is added to the workspace, but the code after the first part(collectionservice:GetInstanceAddedSignal("food"):Connect(function(instance:Part)) doesnt seem to work

make sure you change the attribute on the server side if this script is server side too.

also, you should set cooked to false if you intended that the temperature would keep increasing beyond the max temperature

you may also make it an integer or string to allow different states such as “raw” “medium raw” “medium” “well done” “overcooked” “burnt”