I am trying to make a script that will cycle through models and turn them into a certain material depending on what color it is. Right now I only have the script that will cycle through it, but it won't work. The error it gives me is " Argument 1 missing or nil." It is in a server script. This is the code:

 local S = game.Workspace
local T = game.ServerStorage
function cycle()
	local P = S:FindFirstChild()
	if P:IsA("Model") then
	repeat
	Parts()
	until
	P:FindFirstChild() == nil	
	end
end
function Parts()
	local C = P:FindFirstChild()
	if C:IsA("Part")then
	elseif C == nil then
	P.Parent = T.Analyzed
	repeat
	re()
	until
	T.Analyzing:FindFirstChild() == nil	
	else
	C.Parent = T.Analyzing
	end
end
function re()
	local R = T.Analyzing:FindFirstChild()
	if R ~= nil then
		R.Parent = P		
	end
end
function B()
	local M = T.Analyzed:FindFirstChild()
	if M ~= nil then
		M.Parent = S
	end
end
repeat
	cycle()
	wait(1)
until
S:FindFirstChild() == nil
repeat
B()
wait(1)	
until
T.Analyzed:FindFirstChild() == nil

Edit: I know it won't change the material yet since I have not coded it in yet. I am just trying to see if it will move the materials to the holding folder.