name: Overlay
card_text: Target 1 face-up Xyz Monster your opponent controls that has Xyz Material and 1 face-up Xyz Monster you control; detach all Xyz Materials from the opponent's monster, and if you do, attach this card to your monster as an Xyz Material.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.filter1(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:GetOverlayCount() > 0
  end
  function s.filter2(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter1, tp, 0, LOCATION_MZONE, 1, nil) and Duel.IsExistingTarget(s.filter2, tp, LOCATION_MZONE, 0, 1, nil) and e:GetHandler():IsCanOverlay()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g1 = Duel.SelectTarget(tp, s.filter1, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	e:SetLabelObject(g1:GetFirst())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter2, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc1 = e:GetLabelObject()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc2 = g:GetFirst()
  	if tc1 == tc2 then
  		tc2 = g:GetNext()
  	end
  	if not tc1:IsControler(1 - tp) or not tc1:IsRelateToEffect(e) then
  		return
  	end
  	local og = tc1:GetOverlayGroup()
  	if og:GetCount() == 0 then
  		return
  	end
  	if Duel.SendtoGrave(og, REASON_EFFECT) ~= 0 and tc2:IsControler(tp) and tc2:IsRelateToEffect(e) and c:IsRelateToEffect(e) and c:IsCanOverlay() then
  		c:CancelToGrave()
  		Duel.Overlay(tc2, Group.FromCards(c))
  	end
  end
  
  
source_text: Overlay
  Target 1 face-up Xyz Monster your opponent controls that has Xyz Material and 1 face-up Xyz Monster you control; detach all Xyz Materials from the opponent's monster, and if you do, attach this card to your monster as an Xyz Material.
target_text: -- Overlay
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.filter1(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:GetOverlayCount() > 0
  end
  function s.filter2(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter1, tp, 0, LOCATION_MZONE, 1, nil) and Duel.IsExistingTarget(s.filter2, tp, LOCATION_MZONE, 0, 1, nil) and e:GetHandler():IsCanOverlay()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g1 = Duel.SelectTarget(tp, s.filter1, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	e:SetLabelObject(g1:GetFirst())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter2, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc1 = e:GetLabelObject()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc2 = g:GetFirst()
  	if tc1 == tc2 then
  		tc2 = g:GetNext()
  	end
  	if not tc1:IsControler(1 - tp) or not tc1:IsRelateToEffect(e) then
  		return
  	end
  	local og = tc1:GetOverlayGroup()
  	if og:GetCount() == 0 then
  		return
  	end
  	if Duel.SendtoGrave(og, REASON_EFFECT) ~= 0 and tc2:IsControler(tp) and tc2:IsRelateToEffect(e) and c:IsRelateToEffect(e) and c:IsCanOverlay() then
  		c:CancelToGrave()
  		Duel.Overlay(tc2, Group.FromCards(c))
  	end
  end
  
  