Ghostrick
Cannot be Normal Summoned, unless you control a "Ghostrick" monster. Once per turn: You can change this card to face-down Defense Position. When this card is flipped face-up: You can target 1 "Ghostrick" card in your GY; Set that card (but banish it when it leaves the field), then, you can change face-up monsters your opponent controls to face-down Defense Position, up to the number of Set cards you control.
local SETCARD_ID_0 = 0x8d --Ghostrick

local SETCARD_ID_0 = 0x8d --Ghostrick
local s, id, o = GetID()
function s.initial_effect(c)
	--summon limit
	local e1 = Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_CANNOT_SUMMON)
	e1:SetCondition(s.sumcon)
	c:RegisterEffect(e1)
	--turn set
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetCategory(CATEGORY_POSITION)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetRange(LOCATION_MZONE)
	e2:SetTarget(s.postg)
	e2:SetOperation(s.posop)
	c:RegisterEffect(e2)
	--set card
	local e3 = Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id, 1))
	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_FLIP)
	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e3:SetTarget(s.settg)
	e3:SetOperation(s.setop)
	c:RegisterEffect(e3)
end
function s.sfilter(c)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
end
function s.sumcon(e)
	return not Duel.IsExistingMatchingCard(s.sfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
end
function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
	local c = e:GetHandler()
	if chk == 0 then
		return c:IsCanTurnSet() and c:GetFlagEffect(id) == 0
	end
	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_PHASE + PHASE_END, 0, 1)
	Duel.SetOperationInfo(0, CATEGORY_POSITION, c, 1, 0, 0)
end
function s.posop(e, tp, eg, ep, ev, re, r, rp)
	local c = e:GetHandler()
	if c:IsRelateToEffect(e) and c:IsFaceup() then
		Duel.ChangePosition(c, POS_FACEDOWN_DEFENSE)
	end
end
function s.setfilter(c, e, tp)
	if not c:IsSetCard(SETCARD_ID_0) then
		return false
	end
	if c:IsType(TYPE_MONSTER) then
		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEDOWN_DEFENSE)
	else
		return c:IsSSetable()
	end
end
function s.posfilter(c)
	return c:IsFaceup() and c:IsCanTurnSet()
end
function s.settg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
	if chkc then
		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.setfilter(chkc, e, tp)
	end
	if chk == 0 then
		return Duel.IsExistingTarget(s.setfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
	end
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
	local g = Duel.SelectTarget(tp, s.setfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
	if g:GetFirst():IsType(TYPE_MONSTER) then
		e:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_POSITION)
		Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
	else
		e:SetCategory(CATEGORY_POSITION)
		Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 1, 0, 0)
	end
end
function s.setop(e, tp, eg, ep, ev, re, r, rp)
	local tc = Duel.GetFirstTarget()
	if not tc:IsRelateToEffect(e) then
		return
	end
	local res = 0
	if tc:IsType(TYPE_MONSTER) then
		res = Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEDOWN_DEFENSE)
		if res ~= 0 then
			Duel.ConfirmCards(1 - tp, tc)
		end
	else
		res = Duel.SSet(tp, tc)
	end
	if res ~= 0 then
		local e1 = Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
		e1:SetValue(LOCATION_REMOVED)
		tc:RegisterEffect(e1, true)
		local ct = Duel.GetMatchingGroupCount(Card.IsFacedown, tp, LOCATION_ONFIELD, 0, nil)
		if ct > 0 and Duel.IsExistingMatchingCard(s.posfilter, tp, 0, LOCATION_MZONE, 1, nil) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
			Duel.BreakEffect()
			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
			local g = Duel.SelectMatchingCard(tp, s.posfilter, tp, 0, LOCATION_MZONE, 1, ct, nil)
			Duel.HintSelection(g)
			Duel.ChangePosition(g, POS_FACEDOWN_DEFENSE)
		end
	end
end


Ghostrick
Cannot be Normal Summoned, unless you control a "Ghostrick" monster. Once per turn: You can change this card to face-down Defense Position. When this card is flipped face-up: You can target 1 "Ghostrick" card in your GY; Set that card (but banish it when it leaves the field), then, you can change face-up monsters your opponent controls to face-down Defense Position, up to the number of Set cards you control.
-- Ghostrick
local SETCARD_ID_0 = 0x8d --Ghostrick
local s, id, o = GetID()
function s.initial_effect(c)
	--summon limit
	local e1 = Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_CANNOT_SUMMON)
	e1:SetCondition(s.sumcon)
	c:RegisterEffect(e1)
	--turn set
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetCategory(CATEGORY_POSITION)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetRange(LOCATION_MZONE)
	e2:SetTarget(s.postg)
	e2:SetOperation(s.posop)
	c:RegisterEffect(e2)
	--set card
	local e3 = Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id, 1))
	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_FLIP)
	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e3:SetTarget(s.settg)
	e3:SetOperation(s.setop)
	c:RegisterEffect(e3)
end
function s.sfilter(c)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
end
function s.sumcon(e)
	return not Duel.IsExistingMatchingCard(s.sfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
end
function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
	local c = e:GetHandler()
	if chk == 0 then
		return c:IsCanTurnSet() and c:GetFlagEffect(id) == 0
	end
	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_PHASE + PHASE_END, 0, 1)
	Duel.SetOperationInfo(0, CATEGORY_POSITION, c, 1, 0, 0)
end
function s.posop(e, tp, eg, ep, ev, re, r, rp)
	local c = e:GetHandler()
	if c:IsRelateToEffect(e) and c:IsFaceup() then
		Duel.ChangePosition(c, POS_FACEDOWN_DEFENSE)
	end
end
function s.setfilter(c, e, tp)
	if not c:IsSetCard(SETCARD_ID_0) then
		return false
	end
	if c:IsType(TYPE_MONSTER) then
		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEDOWN_DEFENSE)
	else
		return c:IsSSetable()
	end
end
function s.posfilter(c)
	return c:IsFaceup() and c:IsCanTurnSet()
end
function s.settg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
	if chkc then
		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.setfilter(chkc, e, tp)
	end
	if chk == 0 then
		return Duel.IsExistingTarget(s.setfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
	end
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
	local g = Duel.SelectTarget(tp, s.setfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
	if g:GetFirst():IsType(TYPE_MONSTER) then
		e:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_POSITION)
		Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
	else
		e:SetCategory(CATEGORY_POSITION)
		Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 1, 0, 0)
	end
end
function s.setop(e, tp, eg, ep, ev, re, r, rp)
	local tc = Duel.GetFirstTarget()
	if not tc:IsRelateToEffect(e) then
		return
	end
	local res = 0
	if tc:IsType(TYPE_MONSTER) then
		res = Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEDOWN_DEFENSE)
		if res ~= 0 then
			Duel.ConfirmCards(1 - tp, tc)
		end
	else
		res = Duel.SSet(tp, tc)
	end
	if res ~= 0 then
		local e1 = Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
		e1:SetValue(LOCATION_REMOVED)
		tc:RegisterEffect(e1, true)
		local ct = Duel.GetMatchingGroupCount(Card.IsFacedown, tp, LOCATION_ONFIELD, 0, nil)
		if ct > 0 and Duel.IsExistingMatchingCard(s.posfilter, tp, 0, LOCATION_MZONE, 1, nil) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
			Duel.BreakEffect()
			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
			local g = Duel.SelectMatchingCard(tp, s.posfilter, tp, 0, LOCATION_MZONE, 1, ct, nil)
			Duel.HintSelection(g)
			Duel.ChangePosition(g, POS_FACEDOWN_DEFENSE)
		end
	end
end

