local ageEvent
for _, child in pairs(script.link.Value:GetChildren()) do
	if child:IsA("Model") then
		ageEvent = child.AgeEvent
	end
end

local currentAge = script.currentAge
local endColor = script.endColor.Value
local midColor
if script.midColor.Value ~= Color3.fromRGB(0, 0, 0) then
	midColor = script.midColor.Value
end
local endMaterial = Enum.Material[script.endMaterial.Value]
local ageTillMidColor = script.ageTillMidColor.Value
local ageTillEndColor = script.ageTillEndColor.Value
local ageTillEndMaterial = script.ageTillEndMaterial.Value
local ageTillDeadChildren = script.ageTillDeadChildren.Value
local ageTillDestroy = script.ageTillDestroy.Value
local part = script.Parent
local currentAgeValue

function age(time)
	if script:IsDescendantOf(game.ServerStorage) ~= true then
		currentAge.Value = currentAge.Value + time
		currentAgeValue = currentAge.Value
		if midColor ~= nil then
			part.Color = part.Color:lerp(midColor, math.min(1, currentAgeValue / ageTillMidColor))
			if currentAgeValue >= ageTillMidColor then
				part.Color = part.Color:lerp(endColor, math.min(1, currentAgeValue / ageTillEndColor))
			end
		else
			part.Color = part.Color:lerp(endColor, math.min(1, currentAgeValue / ageTillEndColor))
		end
		if currentAgeValue >= ageTillEndMaterial then
			part.Material = endMaterial
		end
		if currentAgeValue >= ageTillDeadChildren then
			for _, child in pairs(part:GetChildren()) do
				if child ~= script then
					child:Destroy()
				end
				part.Anchored = false
				part.CanCollide = true
			end
		end
		if currentAgeValue >= ageTillDestroy then
			part:Destroy()
		end
	end
end

ageEvent.event:Connect(age)
