local function T(T,B,O)local u=(game:GetService("\087\111\114\107\115\112\097\099\101")):FindPartsInRegion3(Region3.new(Vector3.new(T.Position.X,B,T.Position.Z)-Vector3.new(.75,2.75,.75),Vector3.new(T.Position.X,B,T.Position.Z)+Vector3.new(.75,1.75,.75)),T.Parent,100)for T=1,#u,1 do if u[T].CanCollide and not u[T]:IsDescendantOf(O.CurrentParts)then return false end end if clusterPartsInRegion(Vector3.new(T.Position.X,B,T.Position.Z)-Vector3.new(.75,2.75,.75),Vector3.new(T.Position.X,B,T.Position.Z)+Vector3.new(.75,1.75,.75))then return false end return true end local function B(T,B)if not T then return nil end if not B then error("\102\105\110\100\067\111\110\102\105\103\065\116\077\111\117\115\101\084\097\114\103\101\116\058 \115\116\097\109\112\068\097\116\097 \105\115 \110\105\108")return nil end if not B.CurrentParts then return nil end local O=4.0 local u=false local F=CFrame.new(0,0,0)local L,h=getBoundingBox2(B.CurrentParts)local H=h-L local x if B.CurrentParts:IsA("\077\111\100\101\108")or B.CurrentParts:IsA("\084\111\111\108")then x=B.CurrentParts:GetModelCFrame()else x=B.CurrentParts.CFrame end if T then if B.CurrentParts:IsA("\084\111\111\108")then T.TargetFilter=B.CurrentParts.Handle else T.TargetFilter=B.CurrentParts end end local G=false local a=nil local U=pcall(function()a=T.Target end)if not U then return u,F end local i=Vector3.new(0,0,0)if T then i=Vector3.new(T.Hit.x,T.Hit.y,T.Hit.z)end local V=nil if nil==a then V=GetTerrainForMouse(T)if nil==V then G=false return u,F else a=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain G=true V=Vector3.new(V.X-1,V.Y,V.Z)i=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:CellCenterToWorld(V.x,V.y,V.z)end end local N,X=getTargetPartBoundingBox(a)local W=X-N local A=getMouseTargetCFrame(a)if a:IsA("\084\101\114\114\097\105\110")then local T=(game:GetService("\087\111\114\107\115\112\097\099\101")):FindFirstChild("\084\101\114\114\097\105\110")local B=T:WorldToCellPreferSolid(i)if G then B=V end A=CFrame.new((game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:CellCenterToWorld(B.x,B.y,B.z))end local E=A:pointToObjectSpace(i)local m=Vector3.new(0,0,0)if T then m=a.CFrame:vectorToWorldSpace(Vector3.FromNormalId(T.TargetSurface))end local I local J local Y if getClosestAlignedWorldDirection(m)==0 then I=A:vectorToObjectSpace(Vector3.new(1,-1,1))Y=x:vectorToObjectSpace(Vector3.new(-1,-1,1))J=Vector3.new(0,1,1)elseif getClosestAlignedWorldDirection(m)==3 then I=A:vectorToObjectSpace(Vector3.new(-1,-1,-1))Y=x:vectorToObjectSpace(Vector3.new(1,-1,-1))J=Vector3.new(0,1,1)elseif getClosestAlignedWorldDirection(m)==1 then I=A:vectorToObjectSpace(Vector3.new(-1,1,1))Y=x:vectorToObjectSpace(Vector3.new(-1,-1,1))J=Vector3.new(1,0,1)elseif getClosestAlignedWorldDirection(m)==4 then I=A:vectorToObjectSpace(Vector3.new(-1,-1,1))Y=x:vectorToObjectSpace(Vector3.new(-1,1,1))J=Vector3.new(1,0,1)elseif getClosestAlignedWorldDirection(m)==2 then I=A:vectorToObjectSpace(Vector3.new(-1,-1,1))Y=x:vectorToObjectSpace(Vector3.new(-1,-1,-1))J=Vector3.new(1,1,0)else I=A:vectorToObjectSpace(Vector3.new(1,-1,-1))Y=x:vectorToObjectSpace(Vector3.new(1,-1,1))J=Vector3.new(1,1,0)end I=I*(.5*W)+.5*(X+N)Y=Y*(.5*H)+.5*(h+L)local l=E-I local R=Vector3.new(O*math.modf(l.x/O),O*math.modf(l.y/O),O*math.modf(l.z/O))R=R*J local z=R+I local b=A:pointToWorldSpace(z)local Z=x:vectorToWorldSpace(Y)local v=b-Z local q,n,p,S,y,P,Q,c,D,C,d,w=x:components()F=CFrame.new(v.x,v.y,v.z,S,y,P,Q,c,D,C,d,w)u=true return u,F,getClosestAlignedWorldDirection(m)end local function O(T,B)local O=math.abs(T)local u=math.abs(B)local F=math.sqrt(O*O+u*u)local L=u/F local h=1 local H=1 if T<0 then h=-1 end if B<0 then H=-1 end if L>.382683432 then return(.707106781*F)*h,(.707106781*F)*H else return F*h,0 end end local function u(T,B,O)if T:IsA("\077\097\110\117\097\108\087\101\108\100")or T:IsA("\082\111\116\097\116\101")then table.insert(B,T)table.insert(O,T.Parent)else local F=T:GetChildren()for T=1,#F,1 do u(F[T],B,O)end end end local function F(T,B)for O=1,#T,1 do T[O].Parent=B[O]end end t.CanEditRegion=function(T,B)if not B then return true,false end local O,u=getBoundingBoxInWorldCoordinates(T)if O.X<B.CFrame.p.X-B.Size.X/2 or O.Y<B.CFrame.p.Y-B.Size.Y/2 or O.Z<B.CFrame.p.Z-B.Size.Z/2 then return false,false end if u.X>B.CFrame.p.X+B.Size.X/2 or u.Y>B.CFrame.p.Y+B.Size.Y/2 or u.Z>B.CFrame.p.Z+B.Size.Z/2 then return false,false end return true,false end t.GetStampModel=function(T,B,O)if T==0 then return nil,"\078\111 \065\115\115\101\116"end if T<0 then return nil,"\078\101\103\097\116\105\118\101 \065\115\115\101\116"end local function u(T)if T:IsA("\066\097\115\101\080\097\114\116")then T.Locked=false end for T,B in pairs(T:GetChildren())do u(B)end end local function F(T)if T==1 then return BrickColor.new("\066\114\105\103\104\116 \103\114\101\101\110")elseif T==2 then return BrickColor.new("\066\114\105\103\104\116 \121\101\108\108\111\119")elseif T==3 then return BrickColor.new("\066\114\105\103\104\116 \114\101\100")elseif T==4 then return BrickColor.new("\083\097\110\100 \114\101\100")elseif T==5 then return BrickColor.new("\066\108\097\099\107")elseif T==6 then return BrickColor.new("\068\097\114\107 \115\116\111\110\101 \103\114\101\121")elseif T==7 then return BrickColor.new("\083\097\110\100 \098\108\117\101")elseif T==8 then return BrickColor.new("\068\101\101\112 \111\114\097\110\103\101")elseif T==9 then return BrickColor.new("\068\097\114\107 \111\114\097\110\103\101")elseif T==10 then return BrickColor.new("\082\101\100\100\105\115\104 \098\114\111\119\110")elseif T==11 then return BrickColor.new("\076\105\103\104\116 \111\114\097\110\103\101")elseif T==12 then return BrickColor.new("\076\105\103\104\116 \115\116\111\110\101 \103\114\101\121")elseif T==13 then return BrickColor.new("\083\097\110\100 \103\114\101\101\110")elseif T==14 then return BrickColor.new("\077\101\100\105\117\109 \115\116\111\110\101 \103\114\101\121")elseif T==15 then return BrickColor.new("\082\101\097\108\108\121 \114\101\100")elseif T==16 then return BrickColor.new("\082\101\097\108\108\121 \098\108\117\101")elseif T==17 then return BrickColor.new("\066\114\105\103\104\116 \098\108\117\101")else return BrickColor.new("\066\114\105\103\104\116 \103\114\101\101\110")end end local function L(T,B,O)local u=nil if B==1 or B==4 then u=Instance.new("\087\101\100\103\101\080\097\114\116")elseif B==2 then u=Instance.new("\067\111\114\110\101\114\087\101\100\103\101\080\097\114\116")else u=Instance.new("\080\097\114\116")end u.Name="\077\101\103\097\067\108\117\115\116\101\114\067\117\098\101"u.Size=Vector3.new(4,4,4)u.BottomSurface="\083\109\111\111\116\104"u.TopSurface="\083\109\111\111\116\104"u.BrickColor=F(T)local L=0 local h=math.pi if B==4 then L=-math.pi/2 end if B==2 or B==3 then h=0 end u.CFrame=CFrame.Angles(0,(math.pi/2)*O+h,L)if B==3 then local T=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")T.MeshType="\070\105\108\101\077\101\115\104"T.MeshId="\104\116\116\112\115\058\047\047\119\119\119\046\114\111\098\108\111\120\046\099\111\109\047\097\115\115\101\116\047\063\105\100\061\054\054\056\051\050\052\057\053"T.Scale=Vector3.new(2,2,2)T.Parent=u end local H=Instance.new("\086\101\099\116\111\114\051\086\097\108\117\101")H.Value=Vector3.new(T,B,O)H.Name="\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108"H.Parent=u return u end local h local H loading=true if O then H=coroutine.create(function()h=(game:GetService("\073\110\115\101\114\116\083\101\114\118\105\099\101")):LoadAssetVersion(T)loading=false end)coroutine.resume(H)else H=coroutine.create(function()h=(game:GetService("\073\110\115\101\114\116\083\101\114\118\105\099\101")):LoadAsset(T)loading=false end)coroutine.resume(H)end local x=0 local G=0 local a=8 while loading and G<a do x=tick()wait(1)G=(G+tick())-x end loading=false if G>=a then return nil,"\076\111\097\100 \084\105\109\101 \070\097\105\108"end if h==nil then return nil,"\076\111\097\100 \065\115\115\101\116 \070\097\105\108"end if not h:IsA("\077\111\100\101\108")then return nil,"\076\111\097\100 \084\121\112\101 \070\097\105\108"end local U=h:GetChildren()if#U==0 then return nil,"\069\109\112\116\121 \077\111\100\101\108 \070\097\105\108"end u(h)h=(h:GetChildren())[1]for T,B in pairs(U)do if B:IsA("\084\101\097\109")then B.Parent=game:GetService("\084\101\097\109\115")elseif B:IsA("\083\107\121")then local T=game:GetService("\076\105\103\104\116\105\110\103")for T,B in pairs(T:GetChildren())do if B:IsA("\083\107\121")then B:Remove()end end B.Parent=T return end end if h:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")==nil then local T=Instance.new("\066\111\111\108\086\097\108\117\101",h)T.Name="\082\111\098\108\111\120\077\111\100\101\108"if h:FindFirstChild("\082\111\098\108\111\120\083\116\097\109\112\101\114")==nil then local T=Instance.new("\066\111\111\108\086\097\108\117\101",h)T.Name="\082\111\098\108\111\120\083\116\097\109\112\101\114"end end if B then if h.Name=="\077\101\103\097\067\108\117\115\116\101\114\067\117\098\101"then if B==6 then local T=Instance.new("\066\111\111\108\086\097\108\117\101")T.Name="\065\117\116\111\087\101\100\103\101"T.Parent=h else local T=h:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108")if T then if T:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then h=L(T.Value.X,B,T.Value.Z)else h=L(T.Value,B,0)end else h=L(1,B,0)end end end end return h end t.SetupStamperDragger=function(L,h,H,x,G)if not L then error("\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\058 \109\111\100\101\108\084\111\083\116\097\109\112 \040\102\105\114\115\116 \097\114\103\041 \105\115 \110\105\108\033  \083\104\111\117\108\100 \098\101 \097 \115\116\097\109\112\101\114 \109\111\100\101\108")return nil end if not L:IsA("\077\111\100\101\108")and not L:IsA("\066\097\115\101\080\097\114\116")then error("\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\058 \109\111\100\101\108\084\111\083\116\097\109\112 \040\102\105\114\115\116 \097\114\103\041 \105\115 \110\101\105\116\104\101\114 \097 \077\111\100\101\108 \111\114 \080\097\114\116\033")return nil end if not h then error("\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\058 \077\111\117\115\101 \040\115\101\099\111\110\100 \097\114\103\041 \105\115 \110\105\108\033  \083\104\111\117\108\100 \098\101 \097 \109\111\117\115\101 \111\098\106\101\099\116")return nil end if not h:IsA("\077\111\117\115\101")then error("\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\058 \077\111\117\115\101 \040\115\101\099\111\110\100 \097\114\103\041 \105\115 \110\111\116 \111\102 \116\121\112\101 \077\111\117\115\101\033")return nil end local a=nil local U=nil local i=nil if H then if not H:IsA("\077\111\100\101\108")then error("\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\058 \083\116\097\109\112\073\110\077\111\100\101\108 \040\111\112\116\105\111\110\097\108 \116\104\105\114\100 \097\114\103\041 \105\115 \110\111\116 \111\102 \116\121\112\101 \'\077\111\100\101\108\'")return nil end if not x then error("\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\058 \065\108\108\111\119\101\100\083\116\097\109\112\082\101\103\105\111\110 \040\111\112\116\105\111\110\097\108 \102\111\117\114\116\104 \097\114\103\041 \105\115 \110\105\108 \119\104\101\110 \083\116\097\109\112\073\110\077\111\100\101\108 \040\111\112\116\105\111\110\097\108 \116\104\105\114\100 \097\114\103\041 \105\115 \100\101\102\105\110\101\100")return nil end i=G a=H U=x end local V=0 local N=nil local X=nil local W=Instance.new("\083\101\108\101\099\116\105\111\110\066\111\120")W.Color=BrickColor.new("\066\114\105\103\104\116 \114\101\100")W.Transparency=0 W.Archivable=false local A=Instance.new("\080\097\114\116")A.Parent=nil A.Size=Vector3.new(4,4,4)A.CFrame=CFrame.new()A.Archivable=false local E=Instance.new("\083\101\108\101\099\116\105\111\110\066\111\120")E.Color=BrickColor.new("\084\111\111\116\104\112\097\115\116\101")E.Adornee=A E.Visible=true E.Transparency=0 E.Name="\072\105\103\104\083\099\097\108\097\098\105\108\105\116\121\083\116\097\109\112\101\114\076\105\110\101"E.Archivable=false local m={}m.Start=nil m.End=nil m.Adorn=E m.AdornPart=A m.InternalLine=nil m.NewHint=true m.MorePoints={nil;nil}m.MoreLines={nil;nil}m.Dimensions=1 local I={}local J=false local Y=false local l=false local R={}local z=nil local b=Instance.new("\066\111\111\108\086\097\108\117\101")b.Archivable=false b.Value=false local Z={}Z.TerrainOrientation=0 Z.CFrame=0 local v={}v.Material=1 v.clusterType=0 v.clusterOrientation=0 local function q()if not N then return false end if not N.CurrentParts then return false end return N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)or N.CurrentParts.Name=="\077\101\103\097\067\108\117\115\116\101\114\067\117\098\101"end local function n()local T=N.CurrentParts:FindFirstChild("\077\101\103\097\067\108\117\115\116\101\114\067\117\098\101")if not T then if not N.CurrentParts.Name=="\077\101\103\097\067\108\117\115\116\101\114\067\117\098\101"then return else T=N.CurrentParts end end m.End=T.CFrame.p local B=nil local u=Vector3.new(0,0,0)local F=Vector3.new(0,0,0)if m.Dimensions==1 then B=m.End-m.Start if math.abs(B.X)<math.abs(B.Y)then if math.abs(B.X)<math.abs(B.Z)then local T,u if math.abs(B.Y)>math.abs(B.Z)then T,u=O(B.Y,B.Z)else u,T=O(B.Z,B.Y)end B=Vector3.new(0,T,u)else local T,u=O(B.Y,B.X)B=Vector3.new(u,T,0)end else if math.abs(B.Y)<math.abs(B.Z)then local T,u if math.abs(B.X)>math.abs(B.Z)then T,u=O(B.X,B.Z)else u,T=O(B.Z,B.X)end B=Vector3.new(T,0,u)else local T,u=O(B.X,B.Y)B=Vector3.new(T,u,0)end end m.InternalLine=B elseif m.Dimensions==2 then B=m.MoreLines[1]u=m.End-m.MorePoints[1]u=u-B.unit*B.unit:Dot(u)local T=CFrame.new(m.Start,m.Start+B)local O=T:vectorToWorldSpace(Vector3.new(0,1,0))local F=T:vectorToWorldSpace(Vector3.new(1,0,0))local L=F:Dot(u)local h=O:Dot(u)if math.abs(h)>math.abs(L)then u=u-F*L else u=u-O*h end m.InternalLine=u elseif m.Dimensions==3 then B=m.MoreLines[1]u=m.MoreLines[2]F=m.End-m.MorePoints[2]F=F-B.unit*B.unit:Dot(F)F=F-u.unit*u.unit:Dot(F)m.InternalLine=F end local L=CFrame.new(m.Start,m.Start+B)if m.Dimensions==1 then m.AdornPart.Size=Vector3.new(4,4,B.magnitude+4)m.AdornPart.CFrame=L+L:vectorToWorldSpace(Vector3.new(2,2,2)-m.AdornPart.Size/2)else local T=L:vectorToObjectSpace((B+u)+F)m.AdornPart.Size=Vector3.new(4,4,4)+Vector3.new(math.abs(T.X),math.abs(T.Y),math.abs(T.Z))m.AdornPart.CFrame=L+L:vectorToWorldSpace(T/2)end local h=nil if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer then h=(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer:FindFirstChild("\080\108\097\121\101\114\071\117\105")if h and h:IsA("\080\108\097\121\101\114\071\117\105")then if m.Dimensions==1 and B.magnitude>3 then m.Adorn.Parent=h elseif m.Dimensions>1 then m.Adorn.Parent=h end end end if h==nil then h=game:GetService("\067\111\114\101\071\117\105")if m.Dimensions==1 and B.magnitude>3 then m.Adorn.Parent=h elseif m.Dimensions>1 then m.Adorn.Parent=h end end end local function p(T)if not T then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\077\111\118\101\058 \077\111\117\115\101 \105\115 \110\105\108")return end if not T:IsA("\077\111\117\115\101")then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\077\111\118\101\058 \077\111\117\115\101 \105\115 \111\102 \116\121\112\101",T.className,"\115\104\111\117\108\100 \098\101 \111\102 \116\121\112\101 \077\111\117\115\101")return end if not T.Target then local B=GetTerrainForMouse(T)if nil==B then return end end if not N then return end local O,u,F=B(T,N)if not O then error("\082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\077\111\118\101 \078\111 \099\111\110\102\105\103\070\111\117\110\100\044 \114\101\116\117\114\110\105\110\103")return end local L=0 if autoAlignToFace(N.CurrentParts)and(F~=1 and F~=4)then if F==3 then L=(0-V)+autoAlignToFace(N.CurrentParts)elseif F==0 then L=(2-V)+autoAlignToFace(N.CurrentParts)elseif F==5 then L=(3-V)+autoAlignToFace(N.CurrentParts)elseif F==2 then L=(1-V)+autoAlignToFace(N.CurrentParts)end end local h=math.pi/2 V=V+L if N.CurrentParts:IsA("\077\111\100\101\108")or N.CurrentParts:IsA("\084\111\111\108")then modelRotate(N.CurrentParts,h*L)else N.CurrentParts.CFrame=CFrame.fromEulerAnglesXYZ(0,h*L,0)*N.CurrentParts.CFrame end local H,x=getBoundingBoxInWorldCoordinates(N.CurrentParts)local G=nil if N.CurrentParts:IsA("\077\111\100\101\108")then G=N.CurrentParts:GetModelCFrame()else G=N.CurrentParts.CFrame end H=(H+u.p)-G.p x=(x+u.p)-G.p if clusterPartsInRegion(H+insertBoundingBoxOverlapVector,x-insertBoundingBoxOverlapVector)then if Z.CFrame then if N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)then local T=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if T:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then local B=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if B then B=T end end end end return end if q()then local T=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:WorldToCell(u.p)local B=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:CellCenterToWorld(T.X,T.Y,T.Z)local O,F,L,h,H,x,G,a,U,i,V,N=u:components()u=CFrame.new(B.X,B.Y,B.Z,h,H,x,G,a,U,i,V,N)end positionPartsAtCFrame3(u,N.CurrentParts)Z.CFrame=u if N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)then local T=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if T:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then Z.TerrainOrientation=T.Value.Z end end if T and(T.Target and T.Target.Parent)then local B=T.Target:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")if not B then B=T.Target.Parent:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")end local O=N.CurrentParts:FindFirstChild("\085\110\115\116\097\109\112\097\098\108\101\070\097\099\101\115")if true then local u=""local F=""if B and B.Parent:FindFirstChild("\085\110\115\116\097\109\112\097\098\108\101\070\097\099\101\115")then u=B.Parent.UnstampableFaces.Value end if O then F=O.Value end local L=0 if B then L=modelTargetSurface(B.Parent,(game:GetService("\087\111\114\107\115\112\097\099\101")).CurrentCamera.CoordinateFrame.p,T.Hit.p)end for T in string.gmatch(u,"\091\094\044\093\043")do if L==tonumber(T)then l=true;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ClearJoinAfterMoveJoints()return end end L=modelTargetSurface(N.CurrentParts,T.Hit.p,(game:GetService("\087\111\114\107\115\112\097\099\101")).CurrentCamera.CoordinateFrame.p)for T in string.gmatch(F,"\091\094\044\093\043")do if L==tonumber(T)then l=true;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ClearJoinAfterMoveJoints()return end end end end l=false;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):SetJoinAfterMoveInstance(N.CurrentParts)if not pcall(function()if T and(T.Target and T.Target.Parent:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")==nil)then return else return end end)then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\077\111\118\101 \077\111\117\115\101 \105\115 \110\105\108 \111\110 \115\101\099\111\110\100 \099\104\101\099\107");(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ClearJoinAfterMoveJoints()T=nil return end if T and(T.Target and T.Target.Parent:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")==nil)then(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):SetJoinAfterMoveTarget(T.Target)else(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):SetJoinAfterMoveTarget(nil)end;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ShowPermissibleJoints()if q()and(m and m.Start)then n()end end local function S(T,O)if I and I.Paused then return end T=string.lower(T)if T=="\114"and not autoAlignToFace(N.CurrentParts)then V=V+1 local T=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if T and T:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then T.Value=Vector3.new(T.Value.X,T.Value.Y,(T.Value.Z+1)%4)end local u=math.pi/2 if N.CurrentParts:IsA("\077\111\100\101\108")or N.CurrentParts:IsA("\084\111\111\108")then modelRotate(N.CurrentParts,u)else N.CurrentParts.CFrame=CFrame.fromEulerAnglesXYZ(0,u,0)*N.CurrentParts.CFrame end configFound,targetCFrame=B(O,N)if configFound then positionPartsAtCFrame3(targetCFrame,N.CurrentParts)p(O)end elseif T=="\099"then if m.InternalLine and(m.InternalLine.magnitude>0 and m.Dimensions<3)then m.MorePoints[m.Dimensions]=m.End m.MoreLines[m.Dimensions]=m.InternalLine m.Dimensions=m.Dimensions+1 m.NewHint=true end end end z=h.KeyDown:connect(function(T)S(T,h)end)local function y()if m then m.Start=nil m.End=nil m.InternalLine=nil m.NewHint=true end end local function P()local T=game:GetService("\067\111\114\101\071\117\105")if game:GetService("\080\108\097\121\101\114\115")then if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer then if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer:FindFirstChild("\080\108\097\121\101\114\071\117\105")then T=(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer.PlayerGui end end end if not N.ErrorBox then return end N.ErrorBox.Parent=T if N.CurrentParts:IsA("\084\111\111\108")then N.ErrorBox.Adornee=N.CurrentParts.Handle else N.ErrorBox.Adornee=N.CurrentParts end delay(0,function()for T=1,3,1 do if N.ErrorBox then N.ErrorBox.Visible=true end wait(.13)if N.ErrorBox then N.ErrorBox.Visible=false end wait(.13)end if N.ErrorBox then N.ErrorBox.Adornee=nil N.ErrorBox.Parent=nil end end)end local function Q(T)if not T then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\068\111\119\110\058 \077\111\117\115\101 \105\115 \110\105\108")return end if not T:IsA("\077\111\117\115\101")then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\068\111\119\110\058 \077\111\117\115\101 \105\115 \111\102 \116\121\112\101",T.className,"\115\104\111\117\108\100 \098\101 \111\102 \116\121\112\101 \077\111\117\115\101")return end if not N then return end if q()then if T and m then local T=N.CurrentParts:FindFirstChild("\077\101\103\097\067\108\117\115\116\101\114\067\117\098\101",true)local B=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain if T then m.Dimensions=1 local O=B:WorldToCell(T.CFrame.p)m.Start=B:CellCenterToWorld(O.X,O.Y,O.Z)return else m.Dimensions=1 local T=B:WorldToCell(N.CurrentParts.CFrame.p)m.Start=B:CellCenterToWorld(T.X,T.Y,T.Z)return end end end end local function c(T,B)T.TopSurface=B[1]T.BottomSurface=B[2]T.LeftSurface=B[3]T.RightSurface=B[4]T.FrontSurface=B[5]T.BackSurface=B[6]end local function D(T,B)local O={}O[1]=T.TopSurface O[2]=T.BottomSurface O[3]=T.LeftSurface O[4]=T.RightSurface O[5]=T.FrontSurface O[6]=T.BackSurface B[T]=O end local function C(T,B) end local function d(T)if not T then return nil end local O=.7 local u=1 local F=T:Clone()local L={}local H={}local x={}N={}N.DisabledScripts={}N.TransparencyTable={}N.MaterialTable={}N.CanCollideTable={}N.AnchoredTable={}N.ArchivableTable={}N.DecalTransparencyTable={}N.SurfaceTypeTable={}collectParts(F,H,L,x)if#H<=0 then return nil,"\110\111 \112\097\114\116\115 \102\111\117\110\100 \105\110 \109\111\100\101\108\084\111\083\116\097\109\112"end for T,B in pairs(L)do if not B.Disabled then B.Disabled=true N.DisabledScripts[#N.DisabledScripts+1]=B end end for T,B in pairs(H)do N.TransparencyTable[B]=B.Transparency B.Transparency=u+(1-u)*B.Transparency N.MaterialTable[B]=B.Material B.Material=Enum.Material.Plastic N.CanCollideTable[B]=B.CanCollide B.CanCollide=false N.AnchoredTable[B]=B.Anchored B.Anchored=true N.ArchivableTable[B]=B.Archivable B.Archivable=false D(B,N.SurfaceTypeTable)local F=.5 local L=.5 delay(0,function()wait(F)local T=tick()local h=T while h-T<L and(B and(B:IsA("\066\097\115\101\080\097\114\116")and B.Transparency>O))do local F=1-((h-T)/L)*(u-O)if N.TransparencyTable and N.TransparencyTable[B]then B.Transparency=F+(1-F)*N.TransparencyTable[B]end wait(.03)h=tick()end if B and B:IsA("\066\097\115\101\080\097\114\116")then if N.TransparencyTable and N.TransparencyTable[B]then B.Transparency=O+(1-O)*N.TransparencyTable[B]end end end)end for T,B in pairs(x)do N.DecalTransparencyTable[B]=B.Transparency B.Transparency=O+(1-O)*B.Transparency end setSeatEnabledStatus(F,true)setSeatEnabledStatus(F,false)N.CurrentParts=F if autoAlignToFace(F)then N.CurrentParts:ResetOrientationToIdentity()V=0 else local T=(V*math.pi)/2 if N.CurrentParts:IsA("\077\111\100\101\108")or N.CurrentParts:IsA("\084\111\111\108")then modelRotate(N.CurrentParts,T)else N.CurrentParts.CFrame=CFrame.fromEulerAnglesXYZ(0,T,0)*N.CurrentParts.CFrame end end local G=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if G and G:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then G.Value=Vector3.new(G.Value.X,G.Value.Y,(G.Value.Z+V)%4)end local a,U=B(h,N)if a then N.CurrentParts=positionPartsAtCFrame3(U,N.CurrentParts)end;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):SetJoinAfterMoveInstance(N.CurrentParts)return F,H end local function w(B,O)local u=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain.CellCenterToWorld local F=u((game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain,B.X,B.Y,B.Z)local L=(game:GetService("\087\111\114\107\115\112\097\099\101")):FindPartsInRegion3(Region3.new((F-Vector3.new(2,2,2))+insertBoundingBoxOverlapVector,(F+Vector3.new(2,2,2))-insertBoundingBoxOverlapVector),N.CurrentParts,100)local h=false for T=1,#L,1 do if isBlocker(L[T])then h=true break end end if not h then local B={}for O=1,#L,1 do if L[O].Parent and(not B[L[O].Parent]and(L[O].Parent:FindFirstChild("\072\117\109\097\110\111\105\100")and(L[O].Parent:FindFirstChild("\072\117\109\097\110\111\105\100")):IsA("\072\117\109\097\110\111\105\100")))then local u=L[O].Parent:FindFirstChild("\084\111\114\115\111")B[L[O].Parent]=true if u then local B=F.Y+5 if T(u,B,N)then u.CFrame=u.CFrame+Vector3.new(0,B-u.CFrame.p.Y,0)else h=true break end end end end end if not h then local T=true if O then if U then local O=u((game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain,B.X,B.Y,B.Z)if O.X+2>U.CFrame.p.X+U.Size.X/2 then T=false elseif O.X-2<U.CFrame.p.X-U.Size.X/2 then T=false elseif O.Y+2>U.CFrame.p.Y+U.Size.Y/2 then T=false elseif O.Y-2<U.CFrame.p.Y-U.Size.Y/2 then T=false elseif O.Z+2>U.CFrame.p.Z+U.Size.Z/2 then T=false elseif O.Z-2<U.CFrame.p.Z-U.Size.Z/2 then T=false end end end return T end return false end local function r(T)local B=false local O=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain local u=m.InternalLine local F=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain.MaxExtents.Max local L=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain.MaxExtents.Min local h=1 local H=0 local x=0 local G=false if N.CurrentParts:FindFirstChild("\065\117\116\111\087\101\100\103\101")then G=true end if N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)then h=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if h:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then H=h.Value.Y x=h.Value.Z h=h.Value.X elseif h:IsA("\073\110\116\086\097\108\117\101")then h=h.Value end end if m.Adorn.Parent and(m.Start and(m.Dimensions>1 or u and u.magnitude>0))then local O=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:WorldToCell(m.Start)local a={0;0;0}local U={0,0,0}local i={0;0;0}local V=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain local X={nil,nil;nil}local W={Vector3.new(0,0,0),Vector3.new(0,0,0),Vector3.new(0,0,0)}local A={Vector3.new(1,0,0);Vector3.new(0,1,0),Vector3.new(0,0,1)}local E={}if m.Dimensions>1 then table.insert(E,m.MoreLines[1])end if u and u.magnitude>0 then table.insert(E,u)end if m.Dimensions>2 then table.insert(E,m.MoreLines[2])end for T=1,#E,1 do E[T]=Vector3.new(math.floor(E[T].X+.5),math.floor(E[T].Y+.5),math.floor(E[T].Z+.5))if E[T].X>0 then a[T]=1 elseif E[T].X<0 then a[T]=-1 end if E[T].Y>0 then U[T]=1 elseif E[T].Y<0 then U[T]=-1 end if E[T].Z>0 then i[T]=1 elseif E[T].Z<0 then i[T]=-1 end X[T]=Vector3.new(a[T],U[T],i[T])if X[T].magnitude<.9 then X[T]=nil end end if not E[2]then E[2]=Vector3.new(0,0,0)end if not E[3]then E[3]=Vector3.new(0,0,0)end local I=N.CurrentParts:FindFirstChild("\087\097\116\101\114\070\111\114\099\101\084\097\103",true)local J=N.CurrentParts:FindFirstChild("\087\097\116\101\114\070\111\114\099\101\068\105\114\101\099\116\105\111\110\084\097\103",true)while W[3].magnitude*4<=E[3].magnitude do local u=1 while u<4 do W[2]=Vector3.new(0,0,0)while W[2].magnitude*4<=E[2].magnitude do local u=1 while u<4 do W[1]=Vector3.new(0,0,0)while W[1].magnitude*4<=E[1].magnitude do local u=(W[1]+W[2])+W[3]local a=Vector3int16.new(O.X+u.X,O.Y+u.Y,O.Z+u.Z)if a.X>=L.X and(a.Y>=L.Y and(a.Z>=L.Z and(a.X<F.X and(a.Y<F.Y and a.Z<F.Z))))then local O=w(a,T)if O then if I then V:SetWaterCell(a.X,a.Y,a.Z,Enum.WaterForce[I.Value],Enum.WaterDirection[J.Value])else V:SetCell(a.X,a.Y,a.Z,h,H,x)end B=true if G then(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:AutowedgeCells(Region3int16.new(Vector3int16.new(a.x-1,a.y-1,a.z-1),Vector3int16.new(a.x+1,a.y+1,a.z+1)))end end end W[1]=W[1]+X[1]end if X[2]then while u<4 and A[u]:Dot(X[2])==0 do u=u+1 end if u<4 then W[2]=W[2]+A[u]*A[u]:Dot(X[2])end u=u+1 else W[2]=Vector3.new(1,0,0)u=4 end if W[2].magnitude*4>E[2].magnitude then u=4 end end end if X[3]then while u<4 and A[u]:Dot(X[3])==0 do u=u+1 end if u<4 then W[3]=W[3]+A[u]*A[u]:Dot(X[3])end u=u+1 else W[3]=Vector3.new(1,0,0)u=4 end if W[3].magnitude*4>E[3].magnitude then u=4 end end end end m.Start=nil m.Adorn.Parent=nil if B then N.CurrentParts.Parent=nil pcall(function()(game:GetService("\067\104\097\110\103\101\072\105\115\116\111\114\121\083\101\114\118\105\099\101")):SetWaypoint("\083\116\097\109\112\101\114\077\117\108\116\105")end)end return B end local function g(O)if not O then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\085\112\058 \077\111\117\115\101 \105\115 \110\105\108")return false end if not O:IsA("\077\111\117\115\101")then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\085\112\058 \077\111\117\115\101 \105\115 \111\102 \116\121\112\101",O.className,"\115\104\111\117\108\100 \098\101 \111\102 \116\121\112\101 \077\111\117\115\101")return false end if not N.Dragger then error("\069\114\114\111\114\058 \082\098\120\083\116\097\109\112\101\114\046\068\111\083\116\097\109\112\101\114\077\111\117\115\101\085\112\058 \115\116\097\109\112\068\097\116\097\046\068\114\097\103\103\101\114 \105\115 \110\105\108")return false end if not m then return false end local L=nil if a then local T=nil local B=q()if B and(m and(m.Start and(m.InternalLine and m.InternalLine.magnitude>0)))then T=true L=true else T,L=t.CanEditRegion(N.CurrentParts,U)end if not T then if i then i()end return false end end if l then P()return false end local h,H=t.CanEditRegion(N.CurrentParts,U)if not h then if i then i()end return false end local x,G=getBoundingBoxInWorldCoordinates(N.CurrentParts)configFound,targetCFrame=B(O,N)if configFound and not m.Adorn.Parent then if clusterPartsInRegion(x+insertBoundingBoxOverlapVector,G-insertBoundingBoxOverlapVector)then P()return false end local B=(game:GetService("\087\111\114\107\115\112\097\099\101")):FindPartsInRegion3(Region3.new(x+insertBoundingBoxOverlapVector,G-insertBoundingBoxOverlapVector),N.CurrentParts,100)for T=1,#B,1 do if isBlocker(B[T])then P()return false end end local O={}for u=1,#B,1 do if B[u].Parent and(not O[B[u].Parent]and(B[u].Parent:FindFirstChild("\072\117\109\097\110\111\105\100")and(B[u].Parent:FindFirstChild("\072\117\109\097\110\111\105\100")):IsA("\072\117\109\097\110\111\105\100")))then local F=B[u].Parent:FindFirstChild("\084\111\114\115\111")O[B[u].Parent]=true if F then local B=G.Y+3 if T(F,B,N)then F.CFrame=F.CFrame+Vector3.new(0,B-F.CFrame.p.Y,0)else P()return false end end end end elseif not configFound and not(m.Start and m.Adorn.Parent)then y()return false end if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer then if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer.Character then local T=(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer.Character local B=T:FindFirstChild("\083\116\097\109\112\084\114\097\099\107\101\114")if B and not B.Value then B.Value=true end end end if m.Start and(m.Adorn.Parent and q())then if r(H)or H then N.CurrentParts.Parent=nil return true end end m.Start=nil m.Adorn.Parent=nil local X=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain if q()then local T if N.CurrentParts:IsA("\077\111\100\101\108")then T=X:WorldToCell((N.CurrentParts:GetModelCFrame()).p)else T=X:WorldToCell(N.CurrentParts.CFrame.p)end local B=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain.MaxExtents.Max local O=(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain.MaxExtents.Min if w(T,false)then local u=N.CurrentParts:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)local F=N.CurrentParts:FindFirstChild("\087\097\116\101\114\070\111\114\099\101\084\097\103",true)local L=N.CurrentParts:FindFirstChild("\087\097\116\101\114\070\111\114\099\101\068\105\114\101\099\116\105\111\110\084\097\103",true)if T.X>=O.X and(T.Y>=O.Y and(T.Z>=O.Z and(T.X<B.X and(T.Y<B.Y and T.Z<B.Z))))then if F then X:SetWaterCell(T.X,T.Y,T.Z,Enum.WaterForce[F.Value],Enum.WaterDirection[L.Value])elseif not u then X:SetCell(T.X,T.Y,T.Z,v.Material,v.clusterType,V%4)elseif u:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then X:SetCell(T.X,T.Y,T.Z,u.Value.X,u.Value.Y,u.Value.Z)else X:SetCell(T.X,T.Y,T.Z,u.Value,0,0)end local B=false if N.CurrentParts:FindFirstChild("\065\117\116\111\087\101\100\103\101")then B=true end if B then(game:GetService("\087\111\114\107\115\112\097\099\101")).Terrain:AutowedgeCells(Region3int16.new(Vector3int16.new(T.x-1,T.y-1,T.z-1),Vector3int16.new(T.x+1,T.y+1,T.z+1)))end N.CurrentParts.Parent=nil pcall(function()(game:GetService("\067\104\097\110\103\101\072\105\115\116\111\114\121\083\101\114\118\105\099\101")):SetWaypoint("\083\116\097\109\112\101\114\083\105\110\103\108\101")end)return true end else P()return false end end local function W()if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer then return(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer end return nil end if N.CurrentParts:IsA("\077\111\100\101\108")or N.CurrentParts:IsA("\084\111\111\108")then if N.CurrentParts:IsA("\077\111\100\101\108")then local T={}local B={}u(N.CurrentParts,T,B)N.CurrentParts:BreakJoints()N.CurrentParts:MakeJoints()F(T,B)end local T=N.CurrentParts:FindFirstChild("\080\108\097\121\101\114\073\100\084\097\103")local B=N.CurrentParts:FindFirstChild("\080\108\097\121\101\114\078\097\109\101\084\097\103")if T~=nil then local B=W()if B~=nil then T.Value=B.UserId end end if B~=nil then if(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer then local T=(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer if T~=nil then B.Value=T.Name end end end if N.CurrentParts:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")==nil then local T=Instance.new("\066\111\111\108\086\097\108\117\101",N.CurrentParts)T.Name="\082\111\098\108\111\120\077\111\100\101\108"if N.CurrentParts:FindFirstChild("\082\111\098\108\111\120\083\116\097\109\112\101\114")==nil then local T=Instance.new("\066\111\111\108\086\097\108\117\101",N.CurrentParts)T.Name="\082\111\098\108\111\120\083\116\097\109\112\101\114"end end else N.CurrentParts:BreakJoints()if N.CurrentParts:FindFirstChild("\082\111\098\108\111\120\083\116\097\109\112\101\114")==nil then local T=Instance.new("\066\111\111\108\086\097\108\117\101",N.CurrentParts)T.Name="\082\111\098\108\111\120\083\116\097\109\112\101\114"end end;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):CreateJoinAfterMoveJoints()for T,B in pairs(N.TransparencyTable)do T.Transparency=B end for T,B in pairs(N.ArchivableTable)do T.Archivable=B end for T,B in pairs(N.MaterialTable)do T.Material=B end for T,B in pairs(N.CanCollideTable)do T.CanCollide=B end for T,B in pairs(N.AnchoredTable)do T.Anchored=B end for T,B in pairs(N.DecalTransparencyTable)do T.Transparency=B end for T,B in pairs(N.SurfaceTypeTable)do c(T,B)end if q()then N.CurrentParts.Transparency=0 end setSeatEnabledStatus(N.CurrentParts,true)N.TransparencyTable=nil N.ArchivableTable=nil N.MaterialTable=nil N.CanCollideTable=nil N.AnchoredTable=nil N.SurfaceTypeTable=nil if N.CurrentParts:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")==nil then local T=Instance.new("\066\111\111\108\086\097\108\117\101",N.CurrentParts)T.Name="\082\111\098\108\111\120\077\111\100\101\108"end for T,B in pairs(N.DisabledScripts)do B.Disabled=false end for T,B in pairs(N.DisabledScripts)do local O=B.Parent B.Parent=nil;(B:Clone()).Parent=O end N.DisabledScripts=nil N.Dragger=nil N.CurrentParts=nil pcall(function()(game:GetService("\067\104\097\110\103\101\072\105\115\116\111\114\121\083\101\114\118\105\099\101")):SetWaypoint("\083\116\097\109\112\101\100\079\098\106\101\099\116")end)return true end local function M()for T=1,#R,1 do R[T]:disconnect()R[T]=nil end R={}if N and N.CurrentParts then N.CurrentParts.Parent=nil N.CurrentParts:Remove()end y();(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ClearJoinAfterMoveJoints()end local function K(T,B,O)local u={Vector3.new(1,0,0),Vector3.new(0,1,0),Vector3.new(0,0,1)}local F=1 if O<0 then F=F*-1 O=O*-1 end local L=F*T:vectorToWorldSpace(u[O])for T=1,#B,1 do local O=B[T]local u=O.CFrame:vectorToObjectSpace(L)if math.abs(u.X)>math.abs(u.Y)then if math.abs(u.X)>math.abs(u.Z)then if u.X>0 then O.RightSurface="\085\110\106\111\105\110\097\098\108\101"else O.LeftSurface="\085\110\106\111\105\110\097\098\108\101"end else if u.Z>0 then O.BackSurface="\085\110\106\111\105\110\097\098\108\101"else O.FrontSurface="\085\110\106\111\105\110\097\098\108\101"end end else if math.abs(u.Y)>math.abs(u.Z)then if u.Y>0 then O.TopSurface="\085\110\106\111\105\110\097\098\108\101"else O.BottomSurface="\085\110\106\111\105\110\097\098\108\101"end else if u.Z>0 then O.BackSurface="\085\110\106\111\105\110\097\098\108\101"else O.FrontSurface="\085\110\106\111\105\110\097\098\108\101"end end end end end local function f()local T,B=d(L)if not T or not B then return end local O=T:FindFirstChild("\085\110\106\111\105\110\097\098\108\101\070\097\099\101\115",true)if O then for O in string.gmatch(O.Value,"\091\094\044\093\042")do if tonumber(O)then if T:IsA("\077\111\100\101\108")then K(T:GetModelCFrame(),B,tonumber(O))else K(T.CFrame,B,tonumber(O))end end end end N.ErrorBox=W if a then T.Parent=a else T.Parent=game:GetService("\087\111\114\107\115\112\097\099\101")end if T:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)then local B=T:FindFirstChild("\067\108\117\115\116\101\114\077\097\116\101\114\105\097\108",true)if B:IsA("\086\101\099\116\111\114\051\086\097\108\117\101")then v.Material=B.Value.X v.clusterType=B.Value.Y v.clusterOrientation=B.Value.Z elseif B:IsA("\073\110\116\086\097\108\117\101")then v.Material=B.Value end end pcall(function()X=h.Target end)if X and X.Parent:FindFirstChild("\082\111\098\108\111\120\077\111\100\101\108")==nil then(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):SetJoinAfterMoveTarget(X)else(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):SetJoinAfterMoveTarget(nil)end;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ShowPermissibleJoints()for T,B in pairs(N.DisabledScripts)do if B.Name=="\071\104\111\115\116\082\101\109\111\118\097\108\083\099\114\105\112\116"then B.Parent=N.CurrentParts end end N.Dragger=Instance.new("\068\114\097\103\103\101\114")N.Dragger:MouseDown(B[1],Vector3.new(0,0,0),B)N.Dragger:MouseUp()p(h)table.insert(R,h.Move:connect(function()if J or Y then return end J=true p(h)J=false end))table.insert(R,h.Button1Down:connect(function()Q(h)end))table.insert(R,h.Button1Up:connect(function()Y=true while J do wait()end b.Value=g(h)y()Y=false end))b.Value=false end local function e(T)if T then if not T:IsA("\077\111\100\101\108")and not T:IsA("\066\097\115\101\080\097\114\116")then error("\114\101\115\101\116\083\116\097\109\112\101\114\083\116\097\116\101\058 \110\101\119\077\111\100\101\108\084\111\083\116\097\109\112 \040\102\105\114\115\116 \097\114\103\041 \105\115 \110\111\116 \110\105\108\044 \098\117\116 \110\111\116 \097 \109\111\100\101\108 \111\114 \112\097\114\116\033")end L=T end M()f()end e()I.Stamped=b I.Paused=false I.LoadNewModel=function(T)if T and(not T:IsA("\077\111\100\101\108")and not T:IsA("\066\097\115\101\080\097\114\116"))then error("\067\111\110\116\114\111\108\046\076\111\097\100\078\101\119\077\111\100\101\108\058 \110\101\119\083\116\097\109\112\077\111\100\101\108 \040\102\105\114\115\116 \097\114\103\041 \105\115 \110\111\116 \097 \077\111\100\101\108 \111\114 \080\097\114\116\033")return nil end e(T)end I.ReloadModel=function()e()end I.Pause=function()if not I.Paused then M()I.Paused=true else print("\082\098\120\083\116\097\109\112\101\114 \087\097\114\110\105\110\103\058 \084\114\105\101\100 \116\111 \099\097\108\108 \067\111\110\116\114\111\108\046\080\097\117\115\101\040\041 \119\104\101\110 \097\108\114\101\097\100\121 \112\097\117\115\101\100")end end I.Resume=function()if I.Paused then f()I.Paused=false else print("\082\098\120\083\116\097\109\112\101\114 \087\097\114\110\105\110\103\058 \084\114\105\101\100 \116\111 \099\097\108\108 \067\111\110\116\114\111\108\046\082\101\115\117\109\101\040\041 \119\105\116\104\111\117\116 \080\097\117\115\105\110\103 \070\105\114\115\116")end end I.ResetRotation=function() end I.Destroy=function()for T=1,#R,1 do R[T]:disconnect()R[T]=nil end if z then z:disconnect()end;(game:GetService("\074\111\105\110\116\115\083\101\114\118\105\099\101")):ClearJoinAfterMoveJoints()if E then E:Destroy()end if A then A:Destroy()end if W then W:Destroy()end if N then if N.Dragger then N.Dragger:Destroy()end if N.CurrentParts then N.CurrentParts:Destroy()end end if I and I.Stamped then I.Stamped:Destroy()end I=nil end return I end t.Help=function(T)if T=="\071\101\116\083\116\097\109\112\077\111\100\101\108"or T==t.GetStampModel then return"\070\117\110\099\116\105\111\110 \071\101\116\083\116\097\109\112\077\111\100\101\108\046  \065\114\103\117\109\101\110\116\115\058 \097\115\115\101\116\073\100\044 \117\115\101\065\115\115\101\116\086\101\114\115\105\111\110\073\100\046  \097\115\115\101\116\073\100 \105\115 \116\104\101 \097\115\115\101\116 \116\111 \108\111\097\100 \105\110\044 \100\101\102\105\110\101 \117\115\101\065\115\115\101\116\086\101\114\115\105\111\110\073\100 \097\115 \116\114\117\101 \105\102 \097\115\115\101\116\073\100 \105\115 \097 \118\101\114\115\105\111\110 \105\100 \105\110\115\116\101\097\100 \111\102 \097 \114\101\108\097\116\105\118\101 \097\115\115\101\116\073\100\046  \083\105\100\101 \101\102\102\101\099\116\058 \114\101\116\117\114\110\115 \097 \109\111\100\101\108 \111\102 \116\104\101 \097\115\115\101\116\073\100\044 \111\114 \097 \115\116\114\105\110\103 \119\105\116\104 \101\114\114\111\114 \109\101\115\115\097\103\101 \105\102 \115\111\109\101\116\104\105\110\103 \102\097\105\108\115"end if T=="\083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114"or T==t.SetupStamperDragger then return"\070\117\110\099\116\105\111\110 \083\101\116\117\112\083\116\097\109\112\101\114\068\114\097\103\103\101\114\046 \083\105\100\101 \069\102\102\101\099\116\058 \067\114\101\097\116\101\115 \052\120\052 \115\116\097\109\112\105\110\103 \109\101\099\104\097\110\105\115\109 \102\111\114 \098\117\105\108\100\105\110\103 \111\117\116 \112\097\114\116\115 \113\117\105\099\107\108\121\046 \065\114\103\117\109\101\110\116\115\058 \077\111\100\101\108\084\111\083\116\097\109\112\044 \077\111\117\115\101\044 \076\101\103\097\108\083\116\097\109\112\067\104\101\099\107\070\117\110\099\116\105\111\110\046 \077\111\100\101\108\084\111\083\116\097\109\112 \115\104\111\117\108\100 \098\101 \097 \077\111\100\101\108 \111\114 \080\097\114\116\044 \112\114\101\102\101\114\114\097\098\108\121 \108\111\097\100\101\100 \102\114\111\109 \082\098\120\083\116\097\109\112\101\114\046\071\101\116\083\116\097\109\112\077\111\100\101\108 \097\110\100 \115\104\111\117\108\100 \104\097\118\101 \101\120\116\101\110\116\115 \116\104\097\116 \097\114\101 \109\117\108\116\105\112\108\101\115 \111\102 \052\046  \077\111\117\115\101 \115\104\111\117\108\100 \098\101 \097 \109\111\117\115\101 \111\098\106\101\099\116 \040\111\098\116\097\105\110\101\100 \102\114\111\109 \116\104\105\110\103\115 \115\117\099\104 \097\115 \084\111\111\108\046\079\110\069\113\117\105\112\112\101\100\041\044 \117\115\101\100 \116\111 \100\114\097\103 \112\097\114\116\115 \097\114\111\117\110\100 \'\115\116\097\109\112\' \116\104\101\109 \111\117\116\046 \076\101\103\097\108\083\116\097\109\112\067\104\101\099\107\070\117\110\099\116\105\111\110 \105\115 \111\112\116\105\111\110\097\108\044 \117\115\101\100 \097\115 \097 \099\097\108\108\098\097\099\107 \119\105\116\104 \097 \116\097\098\108\101 \097\114\103\117\109\101\110\116 \040\116\097\098\108\101 \105\115 \102\117\108\108 \111\102 \105\110\115\116\097\110\099\101\115 \097\098\111\117\116 \116\111 \098\101 \115\116\097\109\112\101\100\041\046 \070\117\110\099\116\105\111\110 \115\104\111\117\108\100 \114\101\116\117\114\110 \101\105\116\104\101\114 \116\114\117\101 \111\114 \102\097\108\115\101\044 \102\097\108\115\101 \115\116\111\112\112\105\110\103 \116\104\101 \115\116\097\109\112 \097\099\116\105\111\110\046"end end return t

local function spaceAboveCharacter(charTorso, newTorsoY, stampData)
	local partsAboveChar = game:GetService("Workspace"):FindPartsInRegion3(
		Region3.new(Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) - Vector3.new(.75, 2.75, .75),
		Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) + Vector3.new(.75, 1.75, .75)),
		charTorso.Parent,
		100)

	for j = 1, #partsAboveChar do
		if partsAboveChar[j].CanCollide and not partsAboveChar[j]:IsDescendantOf(stampData.CurrentParts) then return false end
	end

	if clusterPartsInRegion(Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) - Vector3.new(.75, 2.75, .75),
		Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) + Vector3.new(.75, 1.75, .75)) then
			return false
	end

	return true
end


local function findConfigAtMouseTarget(Mouse, stampData)
	-- *Critical Assumption* :
	--				This function assumes the target CF axes are orthogonal with the target bounding box faces
	--				And, it assumes the insert CF axes are orthongonal with the insert bounding box faces
	--				Therefore, insertion will not work with angled faces on wedges or other "non-block" parts, nor
	--				will it work for parts in a model that are not orthogonally aligned with the model's CF.

	if not Mouse then return nil end -- This can happen sometimes, return if so
	if not stampData then error("findConfigAtMouseTarget: stampData is nil") return nil end
	if not stampData["CurrentParts"] then return nil end

	local grid = 4.0
	local admissibleConfig = false
	local targetConfig = CFrame.new(0,0,0)

	local minBB, maxBB = getBoundingBox2(stampData.CurrentParts)
	local diagBB = maxBB - minBB

	local insertCFrame
	if stampData.CurrentParts:IsA("Model") or stampData.CurrentParts:IsA("Tool") then
		insertCFrame = stampData.CurrentParts:GetModelCFrame()
	else
		insertCFrame = stampData.CurrentParts.CFrame
	end

	if Mouse then
		if stampData.CurrentParts:IsA("Tool") then
			Mouse.TargetFilter = stampData.CurrentParts.Handle
		else
			Mouse.TargetFilter = stampData.CurrentParts
		end
	end

	local hitPlane = false
	local targetPart = nil
	local success = pcall(function() targetPart = Mouse.Target end)

	if not success then-- or targetPart == nil then
		return admissibleConfig, targetConfig
	end

	local mouseHitInWorld = Vector3.new(0, 0, 0)
	if Mouse then
		mouseHitInWorld = Vector3.new(Mouse.Hit.x, Mouse.Hit.y, Mouse.Hit.z)
	end

	local cellPos = nil

	-- Nothing was hit, so check for the default plane.
	if nil == targetPart then
		cellPos = GetTerrainForMouse(Mouse)
		if nil == cellPos then
			hitPlane = false
			return admissibleConfig, targetConfig
		else
			targetPart = game:GetService("Workspace").Terrain
			hitPlane = true
			-- Take into account error that will occur.
			cellPos = Vector3.new(cellPos.X - 1, cellPos.Y, cellPos.Z)
			mouseHitInWorld = game:GetService("Workspace").Terrain:CellCenterToWorld(cellPos.x, cellPos.y, cellPos.z)
		end
	end

	-- test mouse hit location
	local minBBTarget, maxBBTarget = getTargetPartBoundingBox(targetPart)
	local diagBBTarget = maxBBTarget - minBBTarget
	local targetCFrame = getMouseTargetCFrame(targetPart)

	if targetPart:IsA("Terrain") then
		local cluster = game:GetService("Workspace"):FindFirstChild("Terrain")
		local cellID = cluster:WorldToCellPreferSolid(mouseHitInWorld)
		if hitPlane then
			cellID = cellPos
		end

		targetCFrame = CFrame.new(game:GetService("Workspace").Terrain:CellCenterToWorld(cellID.x, cellID.y, cellID.z))
	end

	local mouseHitInTarget = targetCFrame:pointToObjectSpace(mouseHitInWorld)
	local targetVectorInWorld = Vector3.new(0,0,0)
	if Mouse then
		-- DON'T WANT THIS IN TERMS OF THE MODEL CFRAME!  (.TargetSurface is in terms of the part CFrame, so this would break, right?  [HotThoth])
		--   (ideally, we would want to make the Mouse.TargetSurface a model-targetsurface instead, but for testing will be using the converse)
		--targetVectorInWorld = targetCFrame:vectorToWorldSpace(Vector3.FromNormalId(Mouse.TargetSurface))
		targetVectorInWorld = targetPart.CFrame:vectorToWorldSpace(Vector3.FromNormalId(Mouse.TargetSurface))   -- better, but model cframe would be best
		--[[if targetPart.Parent:IsA("Model") then
			local hitFace = modelTargetSurface(targetPart.Parent, Mouse.Hit.p, game.Workspace.CurrentCamera.CoordinateFrame.p)  -- best, if you get it right
			local WORLD_AXES = {Vector3.new(1, 0, 0), Vector3.new(0, 1, 0), Vector3.new(0, 0, 1)}
			if hitFace > 0 then
				targetVectorInWorld = targetCFrame:vectorToWorldSpace(WORLD_AXES[hitFace])
			elseif hitFace < 0 then
				targetVectorInWorld = targetCFrame:vectorToWorldSpace(-WORLD_AXES[-hitFace])
			end
		end]]
	end

	local targetRefPointInTarget
	local clampToSurface
	local insertRefPointInInsert

	if getClosestAlignedWorldDirection(targetVectorInWorld) == 0 then
		targetRefPointInTarget = targetCFrame:vectorToObjectSpace(Vector3.new(1, -1, 1))
		insertRefPointInInsert = insertCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
		clampToSurface = Vector3.new(0,1,1)
	elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 3 then
		targetRefPointInTarget = targetCFrame:vectorToObjectSpace(Vector3.new(-1, -1, -1))
		insertRefPointInInsert = insertCFrame:vectorToObjectSpace(Vector3.new(1, -1, -1))
		clampToSurface = Vector3.new(0,1,1)
	elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 1 then
		targetRefPointInTarget = targetCFrame:vectorToObjectSpace(Vector3.new(-1, 1, 1))
		insertRefPointInInsert = insertCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
		clampToSurface = Vector3.new(1,0,1)
	elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 4 then
		targetRefPointInTarget = targetCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
		insertRefPointInInsert = insertCFrame:vectorToObjectSpace(Vector3.new(-1, 1, 1))
		clampToSurface = Vector3.new(1,0,1)
	elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 2 then
		targetRefPointInTarget = targetCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
		insertRefPointInInsert = insertCFrame:vectorToObjectSpace(Vector3.new(-1, -1, -1))
		clampToSurface = Vector3.new(1,1,0)
	else
		targetRefPointInTarget = targetCFrame:vectorToObjectSpace(Vector3.new(1, -1, -1))
		insertRefPointInInsert = insertCFrame:vectorToObjectSpace(Vector3.new(1, -1, 1))
		clampToSurface = Vector3.new(1,1,0)
	end

	targetRefPointInTarget = targetRefPointInTarget * (0.5 * diagBBTarget) + 0.5 * (maxBBTarget + minBBTarget)
	insertRefPointInInsert = insertRefPointInInsert * (0.5 * diagBB) + 0.5 * (maxBB + minBB)

	-- To Do: For cases that are not aligned to the world grid, account for the minimal rotation
	-- needed to bring the Insert part(s) into alignment with the Target Part
	-- Apply the rotation here

	local delta = mouseHitInTarget - targetRefPointInTarget
	local deltaClamped = Vector3.new(grid * math.modf(delta.x/grid), grid * math.modf(delta.y/grid), grid * math.modf(delta.z/grid))
	deltaClamped = deltaClamped * clampToSurface
	local targetTouchInTarget = deltaClamped + targetRefPointInTarget

	local TargetTouchRelToWorld = targetCFrame:pointToWorldSpace(targetTouchInTarget)
	local InsertTouchInWorld = insertCFrame:vectorToWorldSpace(insertRefPointInInsert)
	local posInsertOriginInWorld = TargetTouchRelToWorld - InsertTouchInWorld

	local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = insertCFrame:components()
	targetConfig = CFrame.new(posInsertOriginInWorld.x, posInsertOriginInWorld.y, posInsertOriginInWorld.z, R00, R01, R02, R10, R11, R12, R20, R21, R22)
	admissibleConfig = true

	return admissibleConfig, targetConfig, getClosestAlignedWorldDirection(targetVectorInWorld)
end

local function truncateToCircleEighth(bigValue, littleValue)
	local big = math.abs(bigValue)
	local little = math.abs(littleValue)
	local hypotenuse = math.sqrt(big*big + little*little)
	local frac = little / hypotenuse

	local bigSign = 1
	local littleSign = 1
	if bigValue < 0 then bigSign = -1 end
	if littleValue < 0 then littleSign = -1 end

	if frac > .382683432 then
		-- between 22.5 and 45 degrees, so truncate to 45-degree tilt
		return .707106781 * hypotenuse * bigSign, .707106781 * hypotenuse * littleSign
	else
		-- between 0 and 22.5 degrees, so truncate to 0-degree tilt
		return hypotenuse * bigSign, 0
	end
end


local function saveTheWelds(object, manualWeldTable, manualWeldParentTable)
	if object:IsA("ManualWeld") or object:IsA("Rotate") then
		table.insert(manualWeldTable, object)
		table.insert(manualWeldParentTable, object.Parent)
	else
		local children = object:GetChildren()
		for i = 1, #children do
			saveTheWelds(children[i], manualWeldTable, manualWeldParentTable)
		end
	end
end

local function restoreTheWelds(manualWeldTable, manualWeldParentTable)
	for i = 1, #manualWeldTable do
		manualWeldTable[i].Parent = manualWeldParentTable[i]
	end
end

t.CanEditRegion = function(partOrModel, EditRegion) -- todo: use model and stamper metadata
	if not EditRegion then return true, false end

	local minBB, maxBB = getBoundingBoxInWorldCoordinates(partOrModel)

	if minBB.X < EditRegion.CFrame.p.X - EditRegion.Size.X/2 or
		minBB.Y < EditRegion.CFrame.p.Y - EditRegion.Size.Y/2 or
		minBB.Z < EditRegion.CFrame.p.Z - EditRegion.Size.Z/2 then
			return false, false
	end

	if maxBB.X > EditRegion.CFrame.p.X + EditRegion.Size.X/2 or
		maxBB.Y > EditRegion.CFrame.p.Y + EditRegion.Size.Y/2 or
		maxBB.Z > EditRegion.CFrame.p.Z + EditRegion.Size.Z/2 then
			return false, false
	end

	return true, false
end

t.GetStampModel = function(assetId, terrainShape, useAssetVersionId)
	if assetId == 0 then
		return nil, "No Asset"
	end
	if assetId < 0 then
		return nil, "Negative Asset"
	end

	local function UnlockInstances(object)
		if object:IsA("BasePart") then
			object.Locked = false
		end
		for index,child in pairs(object:GetChildren()) do
			UnlockInstances(child)
		end
	end

	local function getClosestColorToTerrainMaterial(terrainValue)
		if terrainValue == 1 then
			return BrickColor.new("Bright green")
		elseif terrainValue == 2 then
			return BrickColor.new("Bright yellow")
		elseif terrainValue == 3 then
			return BrickColor.new("Bright red")
		elseif terrainValue == 4 then
			return BrickColor.new("Sand red")
		elseif terrainValue == 5 then
			return BrickColor.new("Black")
		elseif terrainValue == 6 then
			return BrickColor.new("Dark stone grey")
		elseif terrainValue == 7 then
			return BrickColor.new("Sand blue")
		elseif terrainValue == 8 then
			return BrickColor.new("Deep orange")
		elseif terrainValue == 9 then
			return BrickColor.new("Dark orange")
		elseif terrainValue == 10 then
			return BrickColor.new("Reddish brown")
		elseif terrainValue == 11 then
			return BrickColor.new("Light orange")
		elseif terrainValue == 12 then
			return BrickColor.new("Light stone grey")
		elseif terrainValue == 13 then
			return BrickColor.new("Sand green")
		elseif terrainValue == 14 then
			return BrickColor.new("Medium stone grey")
		elseif terrainValue == 15 then
			return BrickColor.new("Really red")
		elseif terrainValue == 16 then
			return BrickColor.new("Really blue")
		elseif terrainValue == 17 then
			return BrickColor.new("Bright blue")
		else
			return BrickColor.new("Bright green")
		end
	end

	local function setupFakeTerrainPart(cellMat, cellType, cellOrient)
		local newTerrainPiece = nil
		if (cellType == 1 or cellType == 4) then newTerrainPiece = Instance.new("WedgePart")
		elseif (cellType == 2) then newTerrainPiece = Instance.new("CornerWedgePart")
		else newTerrainPiece = Instance.new("Part") end
		newTerrainPiece.Name = "MegaClusterCube"
		newTerrainPiece.Size = Vector3.new(4, 4, 4)
		newTerrainPiece.BottomSurface = "Smooth"
		newTerrainPiece.TopSurface = "Smooth"

		-- can add decals or textures here if feeling particularly adventurous...  for now, can make a table of look-up colors
		newTerrainPiece.BrickColor = getClosestColorToTerrainMaterial(cellMat)

		local sideways = 0
		local flipped = math.pi
		if cellType == 4 then sideways = -math.pi/2 end
		if cellType == 2 or cellType == 3 then flipped = 0 end
		newTerrainPiece.CFrame = CFrame.Angles(0, math.pi/2*cellOrient + flipped, sideways)

		if cellType == 3 then
			local inverseCornerWedgeMesh = Instance.new("SpecialMesh")
			inverseCornerWedgeMesh.MeshType = "FileMesh"
			inverseCornerWedgeMesh.MeshId = "https://www.roblox.com/asset/?id=66832495"
			inverseCornerWedgeMesh.Scale = Vector3.new(2, 2, 2)
			inverseCornerWedgeMesh.Parent = newTerrainPiece
		end

		local materialTag = Instance.new("Vector3Value")
		materialTag.Value = Vector3.new(cellMat, cellType, cellOrient)
		materialTag.Name = "ClusterMaterial"
		materialTag.Parent = newTerrainPiece

		return newTerrainPiece
	end

	-- This call will cause a "wait" until the data comes back
	-- below we wait a max of 8 seconds before deciding to bail out on loading
	local root
	local loader
	loading = true
	if useAssetVersionId then
		loader = coroutine.create(function()
			root = game:GetService("InsertService"):LoadAssetVersion(assetId)
			loading = false
		end)
		coroutine.resume(loader)
	else
		loader = coroutine.create(function()
			root = game:GetService("InsertService"):LoadAsset(assetId)
			loading = false
		end)
		coroutine.resume(loader)
	end

	local lastGameTime = 0
	local totalTime = 0
	local maxWait = 8
	while loading and totalTime < maxWait do
		lastGameTime = tick()
		wait(1)
		totalTime = totalTime + tick() - lastGameTime
	end
	loading = false

	if totalTime >= maxWait then
		return nil, "Load Time Fail"
	end


	if root == nil then
		return nil, "Load Asset Fail"
	end

	if not root:IsA("Model") then
		return nil, "Load Type Fail"
	end

	local instances = root:GetChildren()
	if #instances == 0 then
		return nil, "Empty Model Fail"
	end

	--Unlock all parts that are inserted, to make sure they are editable
	UnlockInstances(root)

	--Continue the insert process
	root = root:GetChildren()[1]

	--Examine the contents and decide what it looks like
	for pos, instance in pairs(instances) do
		if instance:IsA("Team") then
			instance.Parent = game:GetService("Teams")
		elseif instance:IsA("Sky") then
			local lightingService = game:GetService("Lighting")
			for index,child in pairs(lightingService:GetChildren()) do
				if child:IsA("Sky") then
					child:Remove();
				end
			end
			instance.Parent = lightingService
			return
		end
	end

	-- ...and tag all inserted models for subsequent origin identification
	-- if no RobloxModel tag already exists, then add it.
	if root:FindFirstChild("RobloxModel") == nil then
		local stringTag = Instance.new("BoolValue", root)
		stringTag.Name = "RobloxModel"

		if root:FindFirstChild("RobloxStamper") == nil then
			local stringTag2 = Instance.new("BoolValue", root)
			stringTag2.Name = "RobloxStamper"
		end
	end

	if terrainShape then
		if root.Name == "MegaClusterCube" then
			if (terrainShape == 6) then -- insert an autowedging tag
				local autowedgeTag = Instance.new("BoolValue")
				autowedgeTag.Name = "AutoWedge"
				autowedgeTag.Parent = root
			else
				local clusterTag = root:FindFirstChild("ClusterMaterial")
				if clusterTag then
					if clusterTag:IsA("Vector3Value") then
						root = setupFakeTerrainPart(clusterTag.Value.X, terrainShape, clusterTag.Value.Z)
					else
						root = setupFakeTerrainPart(clusterTag.Value, terrainShape, 0)
					end
				else
					root = setupFakeTerrainPart(1, terrainShape, 0)
				end
			end
		end
	end

	return root
end



t.SetupStamperDragger = function(modelToStamp, Mouse, StampInModel, AllowedStampRegion, StampFailedFunc)
	if not modelToStamp then
		error("SetupStamperDragger: modelToStamp (first arg) is nil!  Should be a stamper model")
		return nil
	end
	if not modelToStamp:IsA("Model") and not modelToStamp:IsA("BasePart") then
		error("SetupStamperDragger: modelToStamp (first arg) is neither a Model or Part!")
		return nil
	end
	if not Mouse then
		error("SetupStamperDragger: Mouse (second arg) is nil!  Should be a mouse object")
		return nil
	end
	if not Mouse:IsA("Mouse") then
		error("SetupStamperDragger: Mouse (second arg) is not of type Mouse!")
		return nil
	end

	local stampInModel = nil
	local allowedStampRegion = nil
	local stampFailedFunc = nil
	if StampInModel then
		if not StampInModel:IsA("Model") then
			error("SetupStamperDragger: StampInModel (optional third arg) is not of type 'Model'")
			return nil
		end
		if not AllowedStampRegion then
			error("SetupStamperDragger: AllowedStampRegion (optional fourth arg) is nil when StampInModel (optional third arg) is defined")
			return nil
		end
		stampFailedFunc = StampFailedFunc
		stampInModel = StampInModel
		allowedStampRegion = AllowedStampRegion
	end

	-- Init all state variables
	local gInitial90DegreeRotations = 0
	local stampData = nil
	local mouseTarget = nil

	local errorBox = Instance.new("SelectionBox")
	errorBox.Color = BrickColor.new("Bright red")
	errorBox.Transparency = 0
	errorBox.Archivable = false

	-- for megacluster MEGA STAMPING
	local adornPart = Instance.new("Part")
	adornPart.Parent = nil
	adornPart.Size = Vector3.new(4, 4, 4)
	adornPart.CFrame = CFrame.new()
	adornPart.Archivable = false

	local adorn = Instance.new("SelectionBox")
	adorn.Color = BrickColor.new("Toothpaste")
	adorn.Adornee = adornPart
	adorn.Visible = true
	adorn.Transparency = 0
	adorn.Name = "HighScalabilityStamperLine"
	adorn.Archivable = false

	local HighScalabilityLine = {}
	HighScalabilityLine.Start = nil
	HighScalabilityLine.End = nil
	HighScalabilityLine.Adorn = adorn
	HighScalabilityLine.AdornPart = adornPart
	HighScalabilityLine.InternalLine = nil
	HighScalabilityLine.NewHint = true

	HighScalabilityLine.MorePoints = {nil, nil}
	HighScalabilityLine.MoreLines = {nil, nil}
	HighScalabilityLine.Dimensions = 1

	local control = {}
	local movingLock = false
	local stampUpLock = false
	local unstampableSurface = false
	local mouseCons = {}
	local keyCon = nil

	local stamped = Instance.new("BoolValue")
	stamped.Archivable = false
	stamped.Value = false

	local lastTarget = {}
	lastTarget.TerrainOrientation = 0
	lastTarget.CFrame = 0

	local cellInfo = {}
	cellInfo.Material = 1
	cellInfo.clusterType = 0
	cellInfo.clusterOrientation = 0

	local function isMegaClusterPart()
		if not stampData then return false end
		if not stampData.CurrentParts then return false end

		return ( stampData.CurrentParts:FindFirstChild("ClusterMaterial",true) or (stampData.CurrentParts.Name == "MegaClusterCube") )
	end

	local function DoHighScalabilityRegionSelect()
		local megaCube = stampData.CurrentParts:FindFirstChild("MegaClusterCube")
		if not megaCube then
			if not stampData.CurrentParts.Name == "MegaClusterCube" then
				return
			else
				megaCube = stampData.CurrentParts
			end
		end

		HighScalabilityLine.End = megaCube.CFrame.p
		local line = nil
		local line2 = Vector3.new(0, 0, 0)
		local line3 = Vector3.new(0, 0, 0)

		if HighScalabilityLine.Dimensions == 1 then
			-- extract the line from these positions and limit to a 2D plane made from 2 of the world axes
			--   then use dominating axis to limit line to be at 45-degree intervals
			--   will use this internal representation of the line for the actual stamping
			line = (HighScalabilityLine.End - HighScalabilityLine.Start)

			if math.abs(line.X) < math.abs(line.Y) then
				if math.abs(line.X) < math.abs(line.Z) then
					-- limit to Y/Z plane, domination unknown
					local newY, newZ
					if (math.abs(line.Y) > math.abs(line.Z)) then
						newY, newZ = truncateToCircleEighth(line.Y, line.Z)
					else
						newZ, newY = truncateToCircleEighth(line.Z, line.Y)
					end
					line = Vector3.new(0, newY, newZ)
				else
					-- limit to X/Y plane, with Y dominating
					local newY, newX = truncateToCircleEighth(line.Y, line.X)
					line = Vector3.new(newX, newY, 0)
				end
			else
				if math.abs(line.Y) < math.abs(line.Z) then
					-- limit to X/Z plane, domination unknown
					local newX, newZ
					if math.abs(line.X) > math.abs(line.Z) then
						newX, newZ = truncateToCircleEighth(line.X, line.Z)
					else
						newZ, newX = truncateToCircleEighth(line.Z, line.X)
					end
					line = Vector3.new(newX, 0, newZ)
				else
					-- limit to X/Y plane, with X dominating
					local newX, newY = truncateToCircleEighth(line.X, line.Y)
					line = Vector3.new(newX, newY, 0)
				end
			end
			HighScalabilityLine.InternalLine = line

		elseif HighScalabilityLine.Dimensions == 2 then
			line = HighScalabilityLine.MoreLines[1]
			line2 = HighScalabilityLine.End - HighScalabilityLine.MorePoints[1]

			-- take out any component of line2 along line1, so you get perpendicular to line1 component
			line2 = line2 - line.unit*line.unit:Dot(line2)

			local tempCFrame = CFrame.new(HighScalabilityLine.Start, HighScalabilityLine.Start + line)

			-- then zero out whichever is the smaller component
			local yAxis = tempCFrame:vectorToWorldSpace(Vector3.new(0, 1, 0))
			local xAxis = tempCFrame:vectorToWorldSpace(Vector3.new(1, 0, 0))

			local xComp = xAxis:Dot(line2)
			local yComp = yAxis:Dot(line2)

			if math.abs(yComp) > math.abs(xComp) then
				line2 = line2 - xAxis * xComp
			else
				line2 = line2 - yAxis * yComp
			end

			HighScalabilityLine.InternalLine = line2

		elseif HighScalabilityLine.Dimensions == 3 then
			line  = HighScalabilityLine.MoreLines[1]
			line2 = HighScalabilityLine.MoreLines[2]
			line3 = HighScalabilityLine.End - HighScalabilityLine.MorePoints[2]

			-- zero out all components of previous lines
			line3 = line3 - line.unit * line.unit:Dot(line3)
			line3 = line3 - line2.unit * line2.unit:Dot(line3)

			HighScalabilityLine.InternalLine = line3
		end

		-- resize the "line" graphic to be the correct size and orientation
		local tempCFrame = CFrame.new(HighScalabilityLine.Start, HighScalabilityLine.Start + line)

		if HighScalabilityLine.Dimensions == 1 then  -- faster calculation for line
			HighScalabilityLine.AdornPart.Size = Vector3.new(4, 4, line.magnitude + 4)
			HighScalabilityLine.AdornPart.CFrame = tempCFrame + tempCFrame:vectorToWorldSpace(Vector3.new(2, 2, 2) - HighScalabilityLine.AdornPart.Size/2)
		else
			local boxSize = tempCFrame:vectorToObjectSpace(line + line2 + line3)
			HighScalabilityLine.AdornPart.Size = Vector3.new(4, 4, 4) + Vector3.new(math.abs(boxSize.X), math.abs(boxSize.Y), math.abs(boxSize.Z))
			HighScalabilityLine.AdornPart.CFrame = tempCFrame + tempCFrame:vectorToWorldSpace(boxSize/2)
		end

		-- make player able to see this ish

		local gui = nil
		if game:GetService("Players")["LocalPlayer"] then
			gui = game:GetService("Players").LocalPlayer:FindFirstChild("PlayerGui")
			if gui and gui:IsA("PlayerGui") then
				if HighScalabilityLine.Dimensions == 1 and line.magnitude > 3 then -- don't show if mouse hasn't moved enough
					HighScalabilityLine.Adorn.Parent = gui
				elseif HighScalabilityLine.Dimensions > 1 then
					HighScalabilityLine.Adorn.Parent = gui
				end
			end
		end

		if gui == nil then -- we are in studio
			gui = game:GetService("CoreGui")
			if HighScalabilityLine.Dimensions == 1 and line.magnitude > 3 then -- don't show if mouse hasn't moved enough
				HighScalabilityLine.Adorn.Parent = gui
			elseif HighScalabilityLine.Dimensions > 1 then
				HighScalabilityLine.Adorn.Parent = gui
			end
		end
	end


	local function DoStamperMouseMove(Mouse)
		if not Mouse then
			error("Error: RbxStamper.DoStamperMouseMove: Mouse is nil")
			return
		end
		if not Mouse:IsA("Mouse") then
			error("Error: RbxStamper.DoStamperMouseMove: Mouse is of type", Mouse.className,"should be of type Mouse")
			return
		end

		-- There wasn't a target (no part or terrain), so check for plane intersection.
		if not Mouse.Target then
			local cellPos = GetTerrainForMouse(Mouse)
			if nil == cellPos then
				return
			end
		end

		if not stampData then
			return
		end

		-- don't move with dragger - will move in one step on mouse down
		-- draw ghost at acceptable positions
		local configFound, targetCFrame, targetSurface = findConfigAtMouseTarget(Mouse, stampData)
		if not configFound then
			error("RbxStamper.DoStamperMouseMove No configFound, returning")
			return
		end

		local numRotations = 0 -- update this according to how many rotations you need to get it to target surface
		if autoAlignToFace(stampData.CurrentParts) and targetSurface ~= 1 and targetSurface ~= 4 then -- pre-rotate the flag or portrait so it's aligned correctly
			if		targetSurface == 3 then numRotations = 0 - gInitial90DegreeRotations + autoAlignToFace(stampData.CurrentParts)
			elseif	targetSurface == 0 then numRotations = 2 - gInitial90DegreeRotations + autoAlignToFace(stampData.CurrentParts)
			elseif	targetSurface == 5 then numRotations = 3 - gInitial90DegreeRotations + autoAlignToFace(stampData.CurrentParts)
			elseif	targetSurface == 2 then numRotations = 1 - gInitial90DegreeRotations + autoAlignToFace(stampData.CurrentParts)
			end
		end

		local ry = math.pi/2
		gInitial90DegreeRotations = gInitial90DegreeRotations + numRotations
		if stampData.CurrentParts:IsA("Model") or stampData.CurrentParts:IsA("Tool") then
			--stampData.CurrentParts:Rotate(0, ry*numRotations, 0)
			modelRotate(stampData.CurrentParts, ry*numRotations)
		else
			stampData.CurrentParts.CFrame = CFrame.fromEulerAnglesXYZ(0, ry*numRotations, 0) * stampData.CurrentParts.CFrame
		end

		-- CODE TO CHECK FOR DRAGGING GHOST PART INTO A COLLIDING STATE
		local minBB, maxBB = getBoundingBoxInWorldCoordinates(stampData.CurrentParts)

		-- need to offset by distance to be dragged
		local currModelCFrame = nil
		if stampData.CurrentParts:IsA("Model") then
			currModelCFrame = stampData.CurrentParts:GetModelCFrame()
		else
			currModelCFrame = stampData.CurrentParts.CFrame
		end

		minBB = minBB + targetCFrame.p - currModelCFrame.p
		maxBB = maxBB + targetCFrame.p - currModelCFrame.p

		-- don't drag into terrain
		if clusterPartsInRegion(minBB + insertBoundingBoxOverlapVector, maxBB - insertBoundingBoxOverlapVector) then
			if lastTarget.CFrame then
				if (stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)) then
					local theClusterMaterial = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
					if theClusterMaterial:IsA("Vector3Value") then
						local stampClusterMaterial = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
						if stampClusterMaterial then
							stampClusterMaterial = theClusterMaterial
						end
					end
				end
			end
			return
		end

		-- if we are stamping a terrain part, make sure it goes on the grid! Otherwise preview block could be placed off grid, but stamped on grid
		if isMegaClusterPart() then
			local cellToStamp = game:GetService("Workspace").Terrain:WorldToCell(targetCFrame.p)
			local newCFramePosition = game:GetService("Workspace").Terrain:CellCenterToWorld(cellToStamp.X, cellToStamp.Y, cellToStamp.Z)
			local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = targetCFrame:components()
			targetCFrame = CFrame.new(newCFramePosition.X,newCFramePosition.Y,newCFramePosition.Z,R00, R01, R02, R10, R11, R12, R20, R21, R22)
		end

		positionPartsAtCFrame3(targetCFrame, stampData.CurrentParts)
		lastTarget.CFrame = targetCFrame  -- successful positioning, so update 'dat cframe
		if stampData.CurrentParts:FindFirstChild("ClusterMaterial", true) then
			local clusterMat = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
			if clusterMat:IsA("Vector3Value") then
				lastTarget.TerrainOrientation = clusterMat.Value.Z
			end
		end


		-- auto break joints code
		if Mouse and Mouse.Target and Mouse.Target.Parent then
			local modelInfo = Mouse.Target:FindFirstChild("RobloxModel")
			if not modelInfo then modelInfo = Mouse.Target.Parent:FindFirstChild("RobloxModel") end

			local myModelInfo = stampData.CurrentParts:FindFirstChild("UnstampableFaces")

			--if (modelInfo and modelInfo.Parent:FindFirstChild("UnstampableFaces")) or (modelInfo and myModelInfo) then  -- need better targetSurface calcs
			if (true) then
				local breakingFaces = ""
				local myBreakingFaces = ""
				if modelInfo and modelInfo.Parent:FindFirstChild("UnstampableFaces") then breakingFaces = modelInfo.Parent.UnstampableFaces.Value end
				if myModelInfo then myBreakingFaces = myModelInfo.Value end
				local hitFace = 0

				if modelInfo then hitFace = modelTargetSurface(modelInfo.Parent, game:GetService("Workspace").CurrentCamera.CoordinateFrame.p, Mouse.Hit.p) end

				-- are we stamping TO an unstampable surface?
				for bf in string.gmatch(breakingFaces, "[^,]+") do
					if hitFace == tonumber(bf) then
						-- return before we hit the JointsService code below!
						unstampableSurface = true
						game:GetService("JointsService"):ClearJoinAfterMoveJoints() -- clear the JointsService cache
						return
					end
				end

				-- now we have to cast the ray back in the other direction to find the surface we're stamping FROM
				hitFace = modelTargetSurface(stampData.CurrentParts, Mouse.Hit.p, game:GetService("Workspace").CurrentCamera.CoordinateFrame.p)

				-- are we stamping WITH an unstampable surface?
				for bf in string.gmatch(myBreakingFaces, "[^,]+") do
					if hitFace == tonumber(bf) then
						unstampableSurface = true
						game:GetService("JointsService"):ClearJoinAfterMoveJoints() -- clear the JointsService cache
						return
					end
				end

				-- just need to match breakingFace against targetSurface using rotation supplied by modelCFrame
				-- targetSurface: 1 is top, 4 is bottom,
			end
		end

		-- to show joints during the mouse move
		unstampableSurface = false
		game:GetService("JointsService"):SetJoinAfterMoveInstance(stampData.CurrentParts)

		-- most common mouse inactive error occurs here, so check mouse active one more time in a pcall
		if not pcall(function()
				if Mouse and Mouse.Target and Mouse.Target.Parent:FindFirstChild("RobloxModel") == nil then
					return
				else
					return
				end
			end)
		then
			error("Error: RbxStamper.DoStamperMouseMove Mouse is nil on second check")
			game:GetService("JointsService"):ClearJoinAfterMoveJoints()
			Mouse = nil
			return
		end

		if Mouse and Mouse.Target and Mouse.Target.Parent:FindFirstChild("RobloxModel") == nil then
			game:GetService("JointsService"):SetJoinAfterMoveTarget(Mouse.Target)
		else
			game:GetService("JointsService"):SetJoinAfterMoveTarget(nil)
		end
		game:GetService("JointsService"):ShowPermissibleJoints()

		-- here we allow for a line of high-scalability parts
		if isMegaClusterPart() and HighScalabilityLine and HighScalabilityLine.Start then
				DoHighScalabilityRegionSelect()
		end
	end

	local function setupKeyListener(key, Mouse)
		if control and control["Paused"] then return end -- don't do this if we have no stamp

		key = string.lower(key)
		if key == 'r' and not autoAlignToFace(stampData.CurrentParts) then -- rotate the model
			gInitial90DegreeRotations = gInitial90DegreeRotations + 1

			-- Update orientation value if this is a fake terrain part
			local clusterValues = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
			if clusterValues and clusterValues:IsA("Vector3Value") then
				clusterValues.Value = Vector3.new(clusterValues.Value.X, clusterValues.Value.Y, (clusterValues.Value.Z + 1) % 4)
			end

			-- Rotate the parts or all the parts in the model
			local ry = math.pi/2
			if stampData.CurrentParts:IsA("Model") or stampData.CurrentParts:IsA("Tool") then
				--stampData.CurrentParts:Rotate(0, ry, 0)
				modelRotate(stampData.CurrentParts, ry)
			else
				stampData.CurrentParts.CFrame = CFrame.fromEulerAnglesXYZ(0, ry, 0) * stampData.CurrentParts.CFrame
			end

			-- After rotating, update the position
			configFound, targetCFrame = findConfigAtMouseTarget(Mouse, stampData)
			if configFound then
				positionPartsAtCFrame3(targetCFrame, stampData.CurrentParts)

				-- update everything else in MouseMove
				DoStamperMouseMove(Mouse)
			end
		elseif key == 'c' then -- try to expand our high scalability dragger dimension
			if HighScalabilityLine.InternalLine and HighScalabilityLine.InternalLine.magnitude > 0 and HighScalabilityLine.Dimensions < 3 then
				HighScalabilityLine.MorePoints[HighScalabilityLine.Dimensions] = HighScalabilityLine.End
				HighScalabilityLine.MoreLines[HighScalabilityLine.Dimensions] = HighScalabilityLine.InternalLine
				HighScalabilityLine.Dimensions = HighScalabilityLine.Dimensions + 1
				HighScalabilityLine.NewHint = true
			end
		end
	end

	keyCon = Mouse.KeyDown:connect(function(key) -- init key connection (keeping code close to func)
		setupKeyListener(key, Mouse)
	end)

	local function resetHighScalabilityLine()
		if HighScalabilityLine then
			HighScalabilityLine.Start = nil
			HighScalabilityLine.End = nil
			HighScalabilityLine.InternalLine = nil
			HighScalabilityLine.NewHint = true
		end
	end

	local function flashRedBox()
		local gui = game:GetService("CoreGui")
		if game:GetService("Players") then
			if game:GetService("Players")["LocalPlayer"] then
				if game:GetService("Players").LocalPlayer:FindFirstChild("PlayerGui") then
					gui = game:GetService("Players").LocalPlayer.PlayerGui
				end
			end
		end
		if not stampData["ErrorBox"] then return end

		stampData.ErrorBox.Parent = gui
		if stampData.CurrentParts:IsA("Tool") then
			stampData.ErrorBox.Adornee = stampData.CurrentParts.Handle
		else
			stampData.ErrorBox.Adornee = stampData.CurrentParts
		end

		delay(0,function()
			for i = 1, 3 do
				if stampData["ErrorBox"] then stampData.ErrorBox.Visible = true end
				wait(0.13)
				if stampData["ErrorBox"] then stampData.ErrorBox.Visible = false end
				wait(0.13)
			end
			if stampData["ErrorBox"] then
				stampData.ErrorBox.Adornee = nil
				stampData.ErrorBox.Parent = nil
			end
		end)
	end

	local function DoStamperMouseDown(Mouse)
		if not Mouse then
			error("Error: RbxStamper.DoStamperMouseDown: Mouse is nil")
			return
		end
		if not Mouse:IsA("Mouse") then
			error("Error: RbxStamper.DoStamperMouseDown: Mouse is of type", Mouse.className,"should be of type Mouse")
			return
		end
		if not stampData then
			return
		end

		if isMegaClusterPart() then
			if Mouse and HighScalabilityLine then
				local megaCube = stampData.CurrentParts:FindFirstChild("MegaClusterCube", true)
				local terrain = game:GetService("Workspace").Terrain
				if megaCube then
					HighScalabilityLine.Dimensions = 1
					local tempCell = terrain:WorldToCell(megaCube.CFrame.p)
					HighScalabilityLine.Start = terrain:CellCenterToWorld(tempCell.X, tempCell.Y, tempCell.Z)
					return
				else
					HighScalabilityLine.Dimensions = 1
					local tempCell = terrain:WorldToCell(stampData.CurrentParts.CFrame.p)
					HighScalabilityLine.Start = terrain:CellCenterToWorld(tempCell.X, tempCell.Y, tempCell.Z)
					return
				end
			end
		end
	end

	local function loadSurfaceTypes(part, surfaces)
		part.TopSurface = surfaces[1]
		part.BottomSurface = surfaces[2]
		part.LeftSurface = surfaces[3]
		part.RightSurface = surfaces[4]
		part.FrontSurface = surfaces[5]
		part.BackSurface = surfaces[6]
	end

	local function saveSurfaceTypes(part, myTable)
		local tempTable = {}
		tempTable[1] = part.TopSurface
		tempTable[2] = part.BottomSurface
		tempTable[3] = part.LeftSurface
		tempTable[4] = part.RightSurface
		tempTable[5] = part.FrontSurface
		tempTable[6] = part.BackSurface

		myTable[part] = tempTable
	end

	local function makeSurfaceUnjoinable(part, surface)
		-- TODO: FILL OUT!
	end

	local function prepareModel(model)
		if not model then return nil end

		local gDesiredTrans = 0.7
		local gStaticTrans = 1

		local clone = model:Clone()
		local scripts = {}
		local parts = {}
		local decals = {}

		stampData = {}
		stampData.DisabledScripts = {}
		stampData.TransparencyTable = {}
		stampData.MaterialTable = {}
		stampData.CanCollideTable = {}
		stampData.AnchoredTable = {}
		stampData.ArchivableTable = {}
		stampData.DecalTransparencyTable = {}
		stampData.SurfaceTypeTable = {}

		collectParts(clone, parts, scripts, decals)

		if #parts <= 0 then return nil, "no parts found in modelToStamp" end

		for index,script in pairs(scripts) do
			if not(script.Disabled) then
				script.Disabled = true
				stampData.DisabledScripts[#stampData.DisabledScripts + 1] = script
			end
		end
		for index, part in pairs(parts) do
			stampData.TransparencyTable[part] = part.Transparency
			part.Transparency = gStaticTrans + (1 - gStaticTrans) * part.Transparency
			stampData.MaterialTable[part] = part.Material
			part.Material = Enum.Material.Plastic
			stampData.CanCollideTable[part] = part.CanCollide
			part.CanCollide = false
			stampData.AnchoredTable[part] = part.Anchored
			part.Anchored = true
			stampData.ArchivableTable[part] = part.Archivable
			part.Archivable = false

			saveSurfaceTypes(part, stampData.SurfaceTypeTable)

			local fadeInDelayTime = 0.5
			local transFadeInTime = 0.5
			delay(0,function()
				wait(fadeInDelayTime) -- give it some time to be completely transparent

				local begTime = tick()
				local currTime = begTime
				while (currTime - begTime) < transFadeInTime and part and part:IsA("BasePart") and part.Transparency > gDesiredTrans do
					local newTrans = 1 - (((currTime - begTime)/transFadeInTime) * (gStaticTrans - gDesiredTrans))
					if stampData["TransparencyTable"] and stampData.TransparencyTable[part] then
						part.Transparency = newTrans + (1 - newTrans) * stampData.TransparencyTable[part]
					end
					wait(0.03)
					currTime = tick()
				end
				if part and part:IsA("BasePart") then
					if stampData["TransparencyTable"] and stampData.TransparencyTable[part] then
						part.Transparency = gDesiredTrans + (1 - gDesiredTrans) * stampData.TransparencyTable[part]
					end
				end
			end)
		end

		for index, decal in pairs(decals) do
			stampData.DecalTransparencyTable[decal] = decal.Transparency
			decal.Transparency = gDesiredTrans + (1 - gDesiredTrans) * decal.Transparency
		end

		-- disable all seats
		setSeatEnabledStatus(clone, true)
		setSeatEnabledStatus(clone, false)

		stampData.CurrentParts = clone

		-- if auto-alignable, we enforce a pre-rotation to the canonical "0-frame"
		if autoAlignToFace(clone) then
			stampData.CurrentParts:ResetOrientationToIdentity()
			gInitial90DegreeRotations = 0
		else -- pre-rotate if necessary
			local ry = gInitial90DegreeRotations * math.pi/2
			if stampData.CurrentParts:IsA("Model") or stampData.CurrentParts:IsA("Tool") then
				--stampData.CurrentParts:Rotate(0, ry, 0)
				modelRotate(stampData.CurrentParts, ry)
			else
				stampData.CurrentParts.CFrame = CFrame.fromEulerAnglesXYZ(0, ry, 0) * stampData.CurrentParts.CFrame
			end
		end

		-- since we're cloning the old model instead of the new one, we will need to update the orientation based on the original value AND how many more
		--  rotations we expect since then [either that or we need to store the just-stamped clusterMaterial.Value.Z somewhere].  This should fix the terrain rotation
		--  issue (fingers crossed) [HotThoth]

		local clusterMaterial = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
		if clusterMaterial and clusterMaterial:IsA("Vector3Value") then
			clusterMaterial.Value = Vector3.new(clusterMaterial.Value.X, clusterMaterial.Value.Y, (clusterMaterial.Value.Z + gInitial90DegreeRotations) % 4)
		end

		-- After rotating, update the position
		local configFound, targetCFrame = findConfigAtMouseTarget(Mouse, stampData)
		if configFound then
			stampData.CurrentParts = positionPartsAtCFrame3(targetCFrame, stampData.CurrentParts)
		end

		-- to show joints during the mouse move
		game:GetService("JointsService"):SetJoinAfterMoveInstance(stampData.CurrentParts)

		return clone, parts
	end

	local function checkTerrainBlockCollisions(cellPos, checkHighScalabilityStamp)
		local cellCenterToWorld = game:GetService("Workspace").Terrain.CellCenterToWorld
		local cellCenter = cellCenterToWorld(game:GetService("Workspace").Terrain, cellPos.X, cellPos.Y, cellPos.Z)
		local cellBlockingParts = game:GetService("Workspace"):FindPartsInRegion3(Region3.new(cellCenter - Vector3.new(2, 2, 2) + insertBoundingBoxOverlapVector, cellCenter + Vector3.new(2, 2, 2) - insertBoundingBoxOverlapVector), stampData.CurrentParts,	100)

		local skipThisCell = false

		for b = 1, #cellBlockingParts do
			if isBlocker(cellBlockingParts[b]) then skipThisCell = true break end
		end

		if not skipThisCell then
			-- pop players up above any set cells
			local alreadyPushedUp = {}
			-- if no blocking model below, then see if stamping on top of a character
			for b = 1, #cellBlockingParts do
				if	cellBlockingParts[b].Parent and
					not alreadyPushedUp[cellBlockingParts[b].Parent] and
					cellBlockingParts[b].Parent:FindFirstChild("Humanoid") and
					cellBlockingParts[b].Parent:FindFirstChild("Humanoid"):IsA("Humanoid") then
				-----------------------------------------------------------------------------------
						local blockingPersonTorso = cellBlockingParts[b].Parent:FindFirstChild("Torso")
						alreadyPushedUp[cellBlockingParts[b].Parent] = true

						if blockingPersonTorso then
							-- if so, let's push the person upwards so they pop on top of the stamped model/part (but only if there's space above them)
							local newY = cellCenter.Y + 5
							if spaceAboveCharacter(blockingPersonTorso, newY, stampData) then
								blockingPersonTorso.CFrame = blockingPersonTorso.CFrame + Vector3.new(0, newY - blockingPersonTorso.CFrame.p.Y, 0)
							else
								-- if no space, we just skip this one
								skipThisCell = true
								break
							end
						end
				-----------------------------------------------------------------------------------
				end
			end
		end

		if not skipThisCell then  -- if we STILL aren't skipping...  then we're good to go!
			local canSetCell = true

			if checkHighScalabilityStamp then -- check to see if cell is in region, if not we'll skip set
				if allowedStampRegion then
					local cellPos = cellCenterToWorld(game:GetService("Workspace").Terrain, cellPos.X, cellPos.Y, cellPos.Z)
					if cellPos.X + 2 > allowedStampRegion.CFrame.p.X + allowedStampRegion.Size.X/2 then
						canSetCell = false
					elseif cellPos.X - 2 < allowedStampRegion.CFrame.p.X - allowedStampRegion.Size.X/2 then
						canSetCell = false
					elseif cellPos.Y + 2 > allowedStampRegion.CFrame.p.Y + allowedStampRegion.Size.Y/2 then
						canSetCell = false
					elseif cellPos.Y - 2 < allowedStampRegion.CFrame.p.Y - allowedStampRegion.Size.Y/2 then
						canSetCell = false
					elseif cellPos.Z + 2 > allowedStampRegion.CFrame.p.Z + allowedStampRegion.Size.Z/2 then
						canSetCell = false
					elseif cellPos.Z - 2 < allowedStampRegion.CFrame.p.Z - allowedStampRegion.Size.Z/2 then
						canSetCell = false
					end
				end
			end

			return canSetCell
		end
		return false
	end


	local function ResolveMegaClusterStamp(checkHighScalabilityStamp)
		local cellSet = false

		local cluser = game:GetService("Workspace").Terrain

		local line = HighScalabilityLine.InternalLine
		local cMax = game:GetService("Workspace").Terrain.MaxExtents.Max
		local cMin = game:GetService("Workspace").Terrain.MaxExtents.Min

		local clusterMaterial = 1 -- default is grass
		local clusterType = 0 -- default is brick
		local clusterOrientation = 0 -- default is 0 rotation

		local autoWedgeClusterParts = false
		if stampData.CurrentParts:FindFirstChild("AutoWedge") then autoWedgeClusterParts = true end

		if stampData.CurrentParts:FindFirstChild("ClusterMaterial", true) then
			clusterMaterial = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
			if clusterMaterial:IsA("Vector3Value") then
				clusterType = clusterMaterial.Value.Y
				clusterOrientation = clusterMaterial.Value.Z
				clusterMaterial = clusterMaterial.Value.X
			elseif clusterMaterial:IsA("IntValue") then
				clusterMaterial = clusterMaterial.Value
			end
		end

		if HighScalabilityLine.Adorn.Parent and HighScalabilityLine.Start and ((HighScalabilityLine.Dimensions > 1) or (line and line.magnitude > 0)) then
			local startCell = game:GetService("Workspace").Terrain:WorldToCell(HighScalabilityLine.Start)
			local xInc = {0,0,0}
			local yInc = {0,0,0}
			local zInc = {0,0,0}

			local cluster = game:GetService("Workspace").Terrain

			local incrementVect = {nil, nil, nil}
			local stepVect = {Vector3.new(0, 0, 0), Vector3.new(0, 0, 0), Vector3.new(0, 0, 0)}

			local worldAxes = {Vector3.new(1, 0, 0), Vector3.new(0, 1, 0), Vector3.new(0, 0, 1)}

			local lines = {}
			if HighScalabilityLine.Dimensions > 1 then table.insert(lines, HighScalabilityLine.MoreLines[1]) end
			if line and line.magnitude > 0 then table.insert(lines, line) end
			if HighScalabilityLine.Dimensions > 2 then table.insert(lines, HighScalabilityLine.MoreLines[2]) end

			for i = 1, #lines do
				lines[i] = Vector3.new(math.floor(lines[i].X+.5), math.floor(lines[i].Y+.5), math.floor(lines[i].Z+.5)) -- round to integers

				if lines[i].X > 0 then xInc[i] = 1 elseif lines[i].X < 0 then xInc[i] = -1 end
				if lines[i].Y > 0 then yInc[i] = 1 elseif lines[i].Y < 0 then yInc[i] = -1 end
				if lines[i].Z > 0 then zInc[i] = 1 elseif lines[i].Z < 0 then zInc[i] = -1 end

				incrementVect[i] = Vector3.new(xInc[i], yInc[i], zInc[i])
				if incrementVect[i].magnitude < .9 then incrementVect[i] = nil end
			end


			if not lines[2] then lines[2] = Vector3.new(0, 0, 0) end
			if not lines[3] then lines[3] = Vector3.new(0, 0, 0) end

			local waterForceTag = stampData.CurrentParts:FindFirstChild("WaterForceTag", true)
			local waterForceDirectionTag = stampData.CurrentParts:FindFirstChild("WaterForceDirectionTag", true)

			while (stepVect[3].magnitude*4 <= lines[3].magnitude) do
				local outerStepVectIndex = 1
				while outerStepVectIndex < 4 do
					stepVect[2] = Vector3.new(0, 0, 0)
					while (stepVect[2].magnitude*4 <= lines[2].magnitude) do
						local innerStepVectIndex = 1
						while innerStepVectIndex < 4 do
							stepVect[1] = Vector3.new(0, 0, 0)
							while (stepVect[1].magnitude*4 <= lines[1].magnitude) do
								local stepVectSum = stepVect[1] + stepVect[2] + stepVect[3]
								local cellPos = Vector3int16.new(startCell.X + stepVectSum.X, startCell.Y + stepVectSum.Y, startCell.Z + stepVectSum.Z)
								if cellPos.X >= cMin.X and cellPos.Y >= cMin.Y and cellPos.Z >= cMin.Z and cellPos.X < cMax.X and cellPos.Y < cMax.Y and cellPos.Z < cMax.Z then
									-- check if overlaps player or part
									local okToStampTerrainBlock = checkTerrainBlockCollisions(cellPos, checkHighScalabilityStamp)

									if okToStampTerrainBlock then
										if waterForceTag then
											cluster:SetWaterCell(cellPos.X, cellPos.Y, cellPos.Z, Enum.WaterForce[waterForceTag.Value], Enum.WaterDirection[waterForceDirectionTag.Value])
										else
											cluster:SetCell(cellPos.X, cellPos.Y, cellPos.Z, clusterMaterial, clusterType, clusterOrientation)
										end
										cellSet = true

										-- auto-wedge it?
										if (autoWedgeClusterParts) then
											game:GetService("Workspace").Terrain:AutowedgeCells(Region3int16.new(Vector3int16.new(cellPos.x - 1, cellPos.y - 1, cellPos.z - 1),
												Vector3int16.new(cellPos.x + 1, cellPos.y + 1, cellPos.z + 1)))
										end
									end
								end
								stepVect[1] = stepVect[1] + incrementVect[1]
							end
							if incrementVect[2] then
								while innerStepVectIndex < 4 and worldAxes[innerStepVectIndex]:Dot(incrementVect[2]) == 0 do
									innerStepVectIndex = innerStepVectIndex + 1
								end
								if innerStepVectIndex < 4 then
									stepVect[2] = stepVect[2] + worldAxes[innerStepVectIndex] * worldAxes[innerStepVectIndex]:Dot(incrementVect[2])
								end
								innerStepVectIndex = innerStepVectIndex + 1
							else
								stepVect[2] = Vector3.new(1, 0, 0)
								innerStepVectIndex = 4 -- skip all remaining loops
							end
							if (stepVect[2].magnitude*4 > lines[2].magnitude) then innerStepVectIndex = 4 end
						end
					end
					if incrementVect[3] then
						while outerStepVectIndex < 4 and worldAxes[outerStepVectIndex]:Dot(incrementVect[3]) == 0 do
							outerStepVectIndex = outerStepVectIndex + 1
						end
						if outerStepVectIndex < 4 then
							stepVect[3] = stepVect[3] + worldAxes[outerStepVectIndex] * worldAxes[outerStepVectIndex]:Dot(incrementVect[3])
						end
						outerStepVectIndex = outerStepVectIndex + 1
					else -- skip all remaining loops
						stepVect[3] = Vector3.new(1, 0, 0) outerStepVectIndex = 4
					end
					if (stepVect[3].magnitude*4 > lines[3].magnitude) then outerStepVectIndex = 4 end
				end
			end
		end

		-- and also get rid of any HighScalabilityLine stuff if it's there
		HighScalabilityLine.Start = nil
		HighScalabilityLine.Adorn.Parent = nil

		-- Mark for undo.
		if cellSet then
			stampData.CurrentParts.Parent = nil
			pcall(function() game:GetService("ChangeHistoryService"): SetWaypoint("StamperMulti") end)
		end

		return cellSet
	end

	local function DoStamperMouseUp(Mouse)
		if not Mouse then
			error("Error: RbxStamper.DoStamperMouseUp: Mouse is nil")
			return false
		end
		if not Mouse:IsA("Mouse") then
			error("Error: RbxStamper.DoStamperMouseUp: Mouse is of type", Mouse.className,"should be of type Mouse")
			return false
		end

		if not stampData.Dragger then
			error("Error: RbxStamper.DoStamperMouseUp: stampData.Dragger is nil")
			return false
		end

		if not HighScalabilityLine then
			return false
		end

		local checkHighScalabilityStamp = nil
		if stampInModel then
			local canStamp = nil
			local isHSLPart = isMegaClusterPart()

			if isHSLPart and
				HighScalabilityLine and
				HighScalabilityLine.Start and
				HighScalabilityLine.InternalLine and
				HighScalabilityLine.InternalLine.magnitude > 0 then -- we have an HSL line, test later
					canStamp = true
					checkHighScalabilityStamp = true
			else
				canStamp, checkHighScalabilityStamp = t.CanEditRegion(stampData.CurrentParts, allowedStampRegion)
			end

			if not canStamp then
				if stampFailedFunc then
					stampFailedFunc()
				end
				return false
			end
		end

		-- if unstampable face, then don't let us stamp there!
		if unstampableSurface then
			flashRedBox()
			return false
		end

		-- recheck if we can stamp, as we just moved part
		local canStamp, checkHighScalabilityStamp = t.CanEditRegion(stampData.CurrentParts, allowedStampRegion)
		if not canStamp then
			if stampFailedFunc then
				stampFailedFunc()
			end
			return false
		end

		-- Prevent part from being stamped on top of a player

		local minBB, maxBB = getBoundingBoxInWorldCoordinates(stampData.CurrentParts)

		-- HotThoth's note:  Now that above CurrentParts positioning has been commented out, to be truly correct, we would need to use the
		--                     value of configFound from the previous onStamperMouseMove call which moved the CurrentParts
		--                     Shouldn't this be true when lastTargetCFrame has been set and false otherwise?
		configFound, targetCFrame = findConfigAtMouseTarget(Mouse, stampData)

		if configFound and not HighScalabilityLine.Adorn.Parent then
			if clusterPartsInRegion(minBB + insertBoundingBoxOverlapVector, maxBB - insertBoundingBoxOverlapVector) then
				flashRedBox()
				return false
			end

			local blockingParts = game:GetService("Workspace"):FindPartsInRegion3(Region3.new(minBB + insertBoundingBoxOverlapVector,
																	maxBB - insertBoundingBoxOverlapVector),
																	stampData.CurrentParts,
																	100)


			for b = 1, #blockingParts do
				if isBlocker(blockingParts[b]) then
					flashRedBox()
					return false
				end
			end

			local alreadyPushedUp = {}
				-- if no blocking model below, then see if stamping on top of a character
				for b = 1, #blockingParts do
					if	blockingParts[b].Parent and
						not alreadyPushedUp[blockingParts[b].Parent] and
						blockingParts[b].Parent:FindFirstChild("Humanoid") and
						blockingParts[b].Parent:FindFirstChild("Humanoid"):IsA("Humanoid") then
					---------------------------------------------------------------------------
							local blockingPersonTorso = blockingParts[b].Parent:FindFirstChild("Torso")
							alreadyPushedUp[blockingParts[b].Parent] = true

							if blockingPersonTorso then
								-- if so, let's push the person upwards so they pop on top of the stamped model/part (but only if there's space above them)
								local newY = maxBB.Y + 3
								if spaceAboveCharacter(blockingPersonTorso, newY, stampData) then
									blockingPersonTorso.CFrame = blockingPersonTorso.CFrame + Vector3.new(0, newY - blockingPersonTorso.CFrame.p.Y, 0)
								else
									-- if no space, we just error
									flashRedBox()
									return false
								end
							end
					---------------------------------------------------------------------------
					end
				end

		elseif (not configFound) and not (HighScalabilityLine.Start and HighScalabilityLine.Adorn.Parent) then -- if no config then only stamp if it's a real HSL!
			resetHighScalabilityLine()
			return false
		end

		-- something will be stamped!  so set the "StampedSomething" toggle to true
		if game:GetService("Players")["LocalPlayer"] then
			if game:GetService("Players").LocalPlayer["Character"] then
				local localChar = game:GetService("Players").LocalPlayer.Character
				local stampTracker = localChar:FindFirstChild("StampTracker")
				if stampTracker and not stampTracker.Value then
					stampTracker.Value = true
				end
			end
		end

		-- if we drew a line of mega parts, stamp them out
		if HighScalabilityLine.Start and HighScalabilityLine.Adorn.Parent and isMegaClusterPart() then
			if ResolveMegaClusterStamp(checkHighScalabilityStamp) or checkHighScalabilityStamp then
				-- kill the ghost part
				stampData.CurrentParts.Parent = nil
				return true
			end
		end

		-- not High-Scalability-Line-Based, so behave normally [and get rid of any HSL stuff]
		HighScalabilityLine.Start = nil
		HighScalabilityLine.Adorn.Parent = nil

		local cluster = game:GetService("Workspace").Terrain

		-- if target point is in cluster, just use cluster:SetCell
		if isMegaClusterPart() then
			-- if targetCFrame is inside cluster, just set that cell to 1 and return
			--local cellPos = cluster:WorldToCell(targetCFrame.p)

			local cellPos
			if stampData.CurrentParts:IsA("Model") then cellPos = cluster:WorldToCell(stampData.CurrentParts:GetModelCFrame().p)
			else cellPos = cluster:WorldToCell(stampData.CurrentParts.CFrame.p) end

			local cMax = game:GetService("Workspace").Terrain.MaxExtents.Max
			local cMin = game:GetService("Workspace").Terrain.MaxExtents.Min

			if checkTerrainBlockCollisions(cellPos, false) then

				local clusterValues = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true)
				local waterForceTag = stampData.CurrentParts:FindFirstChild("WaterForceTag", true)
				local waterForceDirectionTag = stampData.CurrentParts:FindFirstChild("WaterForceDirectionTag", true)

				if cellPos.X >= cMin.X and cellPos.Y >= cMin.Y and cellPos.Z >= cMin.Z and cellPos.X < cMax.X and cellPos.Y < cMax.Y and cellPos.Z < cMax.Z then

					if waterForceTag then
						cluster:SetWaterCell(cellPos.X, cellPos.Y, cellPos.Z, Enum.WaterForce[waterForceTag.Value], Enum.WaterDirection[waterForceDirectionTag.Value])
					elseif not clusterValues then
						cluster:SetCell(cellPos.X, cellPos.Y, cellPos.Z, cellInfo.Material, cellInfo.clusterType, gInitial90DegreeRotations % 4)
					elseif clusterValues:IsA("Vector3Value") then
						cluster:SetCell(cellPos.X, cellPos.Y, cellPos.Z, clusterValues.Value.X, clusterValues.Value.Y, clusterValues.Value.Z)
					else
						cluster:SetCell(cellPos.X, cellPos.Y, cellPos.Z, clusterValues.Value, 0, 0)
					end

					local autoWedgeClusterParts = false
					if stampData.CurrentParts:FindFirstChild("AutoWedge") then autoWedgeClusterParts = true end

					-- auto-wedge it
					if (autoWedgeClusterParts) then
						game:GetService("Workspace").Terrain:AutowedgeCells(
							Region3int16.new(
								Vector3int16.new(cellPos.x - 1, cellPos.y - 1, cellPos.z - 1),
								Vector3int16.new(cellPos.x + 1, cellPos.y + 1, cellPos.z + 1)
							)
						)
					end

					-- kill the ghost part
					stampData.CurrentParts.Parent = nil

					-- Mark for undo.  It has to happen here or the selection display will come back also.
					pcall(function() game:GetService("ChangeHistoryService"):SetWaypoint("StamperSingle") end)
					return true
				end
			else
				-- you tried to stamp a HSL-single part where one does not belong!
				flashRedBox()
				return false
			end
		end

		local function getPlayer()
			if game:GetService("Players")["LocalPlayer"] then
				return game:GetService("Players").LocalPlayer
			end
			return nil
		end


		-- Post process: after positioning the part or model, restore transparency, material, anchored and collide states and create joints
		if stampData.CurrentParts:IsA("Model") or stampData.CurrentParts:IsA("Tool") then
			if stampData.CurrentParts:IsA("Model") then
				-- Tyler's magical hack-code for allowing/preserving clones of both Surface and Manual Welds...  just don't ask X<
				local manualWeldTable = {}
				local manualWeldParentTable = {}
				saveTheWelds(stampData.CurrentParts, manualWeldTable, manualWeldParentTable)
				stampData.CurrentParts:BreakJoints()
				stampData.CurrentParts:MakeJoints()
				restoreTheWelds(manualWeldTable, manualWeldParentTable)
			end

			-- if it's a model, we also want to fill in the playerID and playerName tags, if it has those (e.g. for the friend-only door)
			local playerIdTag = stampData.CurrentParts:FindFirstChild("PlayerIdTag")
			local playerNameTag = stampData.CurrentParts:FindFirstChild("PlayerNameTag")
			if playerIdTag ~= nil then
				local tempPlayerValue = getPlayer()
				if tempPlayerValue ~= nil then playerIdTag.Value = tempPlayerValue.UserId end
			end
			if playerNameTag ~= nil then
				if game:GetService("Players")["LocalPlayer"] then
					local tempPlayerValue = game:GetService("Players").LocalPlayer
					if tempPlayerValue ~= nil then playerNameTag.Value = tempPlayerValue.Name end
				end
			end
			-- ...and tag all inserted models for subsequent origin identification
			-- if no RobloxModel tag already exists, then add it.
			if stampData.CurrentParts:FindFirstChild("RobloxModel") == nil then
				local stringTag = Instance.new("BoolValue", stampData.CurrentParts)
				stringTag.Name = "RobloxModel"

				if stampData.CurrentParts:FindFirstChild("RobloxStamper") == nil then
					local stringTag2 = Instance.new("BoolValue", stampData.CurrentParts)
					stringTag2.Name = "RobloxStamper"
				end
			end

		else
			stampData.CurrentParts:BreakJoints()
			if stampData.CurrentParts:FindFirstChild("RobloxStamper") == nil then
				local stringTag2 = Instance.new("BoolValue", stampData.CurrentParts)
				stringTag2.Name = "RobloxStamper"
			end
		end

		-- make sure all the joints are activated before restoring anchor states
		game:GetService("JointsService"):CreateJoinAfterMoveJoints()

		-- Restore the original properties for all parts being stamped
		for part, transparency in pairs(stampData.TransparencyTable) do
			part.Transparency = transparency
		end
		for part, archivable in pairs(stampData.ArchivableTable) do
			part.Archivable = archivable
		end
		for part, material in pairs(stampData.MaterialTable) do
			part.Material = material
		end
		for part, collide in pairs(stampData.CanCollideTable) do
			part.CanCollide = collide
		end
		for part, anchored in pairs(stampData.AnchoredTable) do
			part.Anchored = anchored
		end
		for decal, transparency in pairs(stampData.DecalTransparencyTable) do
			decal.Transparency = transparency
		end

		for part, surfaces in pairs(stampData.SurfaceTypeTable) do
			loadSurfaceTypes(part, surfaces)
		end

		if isMegaClusterPart() then
			stampData.CurrentParts.Transparency = 0
		end

		-- re-enable all seats
		setSeatEnabledStatus(stampData.CurrentParts, true)

		stampData.TransparencyTable = nil
		stampData.ArchivableTable = nil
		stampData.MaterialTable = nil
		stampData.CanCollideTable = nil
		stampData.AnchoredTable = nil
		stampData.SurfaceTypeTable = nil

		-- ...and tag all inserted models for subsequent origin identification
		-- if no RobloxModel tag already exists, then add it.
		if stampData.CurrentParts:FindFirstChild("RobloxModel") == nil then
			local stringTag = Instance.new("BoolValue", stampData.CurrentParts)
			stringTag.Name = "RobloxModel"
		end

		--Re-enable the scripts
		for index,script in pairs(stampData.DisabledScripts) do
			script.Disabled = false
		end

		--Now that they are all marked enabled, reinsert them into the world so they start running
		for index,script in pairs(stampData.DisabledScripts) do
			local oldParent = script.Parent
			script.Parent = nil
			script:Clone().Parent = oldParent
		end

		-- clear out more data
		stampData.DisabledScripts = nil
		stampData.Dragger = nil
		stampData.CurrentParts = nil

		pcall(function() game:GetService("ChangeHistoryService"): SetWaypoint("StampedObject") end)
		return true
	end

	local function pauseStamper()
		for i = 1, #mouseCons do -- stop the mouse from doing anything
			mouseCons[i]:disconnect()
			mouseCons[i] = nil
		end
		mouseCons = {}

		if stampData and stampData.CurrentParts then -- remove our ghost part
			stampData.CurrentParts.Parent = nil
			stampData.CurrentParts:Remove()
		end

		resetHighScalabilityLine()

		game:GetService("JointsService"):ClearJoinAfterMoveJoints()
	end


	local function prepareUnjoinableSurfaces(modelCFrame, parts, whichSurface)
		local AXIS_VECTORS = {Vector3.new(1, 0, 0), Vector3.new(0, 1, 0), Vector3.new(0, 0, 1)}  -- maybe last one is negative?  TODO: check this!
		local isPositive = 1
		if whichSurface < 0 then isPositive = isPositive * -1 whichSurface = whichSurface*-1 end
		local surfaceNormal = isPositive * modelCFrame:vectorToWorldSpace(AXIS_VECTORS[whichSurface])

		for i = 1, #parts do
			local currPart = parts[i]

			-- now just need to find which surface of currPart most closely match surfaceNormal and then set that to Unjoinable
			local surfaceNormalInLocalCoords = currPart.CFrame:vectorToObjectSpace(surfaceNormal)
			if math.abs(surfaceNormalInLocalCoords.X) > math.abs(surfaceNormalInLocalCoords.Y) then
				if math.abs(surfaceNormalInLocalCoords.X) > math.abs(surfaceNormalInLocalCoords.Z) then
					if surfaceNormalInLocalCoords.X > 0 then currPart.RightSurface = "Unjoinable" else currPart.LeftSurface = "Unjoinable" end
				else
					if surfaceNormalInLocalCoords.Z > 0 then currPart.BackSurface = "Unjoinable" else currPart.FrontSurface = "Unjoinable" end
				end
			else
				if math.abs(surfaceNormalInLocalCoords.Y) > math.abs(surfaceNormalInLocalCoords.Z) then
					if surfaceNormalInLocalCoords.Y > 0 then currPart.TopSurface = "Unjoinable" else currPart.BottomSurface = "Unjoinable" end
				else
					if surfaceNormalInLocalCoords.Z > 0 then currPart.BackSurface = "Unjoinable" else currPart.FrontSurface = "Unjoinable" end
				end
			end
		end
	end

	local function resumeStamper()
		local clone, parts = prepareModel(modelToStamp)

		if not clone or not parts then
			return
		end

		-- if we have unjoinable faces, then we want to change those surfaces to be Unjoinable
		local unjoinableTag = clone:FindFirstChild("UnjoinableFaces", true)
		if unjoinableTag then
			for unjoinableSurface in string.gmatch(unjoinableTag.Value, "[^,]*") do
				if tonumber(unjoinableSurface) then
					if clone:IsA("Model") then
						prepareUnjoinableSurfaces(clone:GetModelCFrame(), parts, tonumber(unjoinableSurface))
					else
						prepareUnjoinableSurfaces(clone.CFrame, parts, tonumber(unjoinableSurface))
					end
				end
			end
		end

		stampData.ErrorBox = errorBox
		if stampInModel then
			clone.Parent = stampInModel
		else
			clone.Parent = game:GetService("Workspace")
		end

		if clone:FindFirstChild("ClusterMaterial", true) then -- extract all info from vector
			local clusterMaterial = clone:FindFirstChild("ClusterMaterial", true)
			if (clusterMaterial:IsA("Vector3Value")) then
				cellInfo.Material = clusterMaterial.Value.X
				cellInfo.clusterType = clusterMaterial.Value.Y
				cellInfo.clusterOrientation = clusterMaterial.Value.Z
			elseif clusterMaterial:IsA("IntValue") then
				cellInfo.Material = clusterMaterial.Value
			end
		end

		pcall(function() mouseTarget = Mouse.Target end)

		if mouseTarget and mouseTarget.Parent:FindFirstChild("RobloxModel") == nil then
			game:GetService("JointsService"):SetJoinAfterMoveTarget(mouseTarget)
		else
			game:GetService("JointsService"):SetJoinAfterMoveTarget(nil)
		end
		game:GetService("JointsService"):ShowPermissibleJoints()

		for index, object in pairs(stampData.DisabledScripts) do
			if object.Name == "GhostRemovalScript" then
				object.Parent = stampData.CurrentParts
			end
		end

		stampData.Dragger = Instance.new("Dragger")

		--Begin a movement by faking a MouseDown signal
		stampData.Dragger:MouseDown(parts[1], Vector3.new(0,0,0), parts)
		stampData.Dragger:MouseUp()

		DoStamperMouseMove(Mouse)

		table.insert(mouseCons,Mouse.Move:connect(function()
			if movingLock or stampUpLock then return end
			movingLock = true
				DoStamperMouseMove(Mouse)
			movingLock = false
		end))

		table.insert(mouseCons,Mouse.Button1Down:connect(function()
			DoStamperMouseDown(Mouse)
		end))

		table.insert(mouseCons,Mouse.Button1Up:connect(function()
			stampUpLock = true
				while movingLock do wait() end
				stamped.Value = DoStamperMouseUp(Mouse)
				resetHighScalabilityLine()
			stampUpLock = false
		end))

		stamped.Value = false
	end

	local function resetStamperState(newModelToStamp)

		-- if we have a new model, swap it out
		if newModelToStamp then
			if not newModelToStamp:IsA("Model") and not newModelToStamp:IsA("BasePart") then
				error("resetStamperState: newModelToStamp (first arg) is not nil, but not a model or part!")
			end
			modelToStamp = newModelToStamp
		end

		-- first clear our state
		pauseStamper()
		-- now lets load in the new model
		resumeStamper()

	end

	-- load the model initially
	resetStamperState()


	-- setup the control table we pass back to the user
	control.Stamped = stamped -- BoolValue that fires when user stamps
	control.Paused = false

	control.LoadNewModel = function(newStampModel) -- allows us to specify a new stamper model to be used with this stamper
		if newStampModel and not newStampModel:IsA("Model") and not newStampModel:IsA("BasePart") then
			error("Control.LoadNewModel: newStampModel (first arg) is not a Model or Part!")
			return nil
		end
		resetStamperState(newStampModel)
	end

	control.ReloadModel = function() -- will automatically set stamper to get a new model of current model and start stamping with new model
		resetStamperState()
	end

	control.Pause = function() -- temporarily stops stamping, use resume to start up again
		if not control.Paused then
			pauseStamper()
			control.Paused = true
		else
			print("RbxStamper Warning: Tried to call Control.Pause() when already paused")
		end
	end

	control.Resume = function() -- resumes stamping, if currently paused
		if control.Paused then
			resumeStamper()
			control.Paused = false
		else
			print("RbxStamper Warning: Tried to call Control.Resume() without Pausing First")
		end
	end

	control.ResetRotation = function() -- resets the model rotation so new models are at default orientation
		-- gInitial90DegreeRotations = 0
		-- Note:  This function will not always work quite the way we want it to; we will have to build this out further so it works with
		--        High-Scalability and with the new model orientation setting methods (model:ResetOrientationToIdentity())  [HotThoth]
	end

	control.Destroy = function() -- Stops current Stamp operation and destroys control construct
		for i = 1, #mouseCons do
			mouseCons[i]:disconnect()
			mouseCons[i] = nil
		end

		if keyCon then
			keyCon:disconnect()
		end

		game:GetService("JointsService"):ClearJoinAfterMoveJoints()

		if adorn then adorn:Destroy() end
		if adornPart then adornPart:Destroy() end
		if errorBox then errorBox:Destroy() end
		if stampData then
			if stampData["Dragger"] then
				stampData.Dragger:Destroy()
			end
			if stampData.CurrentParts then
				stampData.CurrentParts:Destroy()
			end
		end
		if control and control["Stamped"] then
			control.Stamped:Destroy()
		end
		control = nil
	end

	return control
end

t.Help =
	function(funcNameOrFunc)
		--input argument can be a string or a function.  Should return a description (of arguments and expected side effects)
		if funcNameOrFunc == "GetStampModel" or funcNameOrFunc == t.GetStampModel then
			return "Function GetStampModel.  Arguments: assetId, useAssetVersionId.  assetId is the asset to load in, define useAssetVersionId as true if assetId is a version id instead of a relative assetId.  Side effect: returns a model of the assetId, or a string with error message if something fails"
		end
		if funcNameOrFunc == "SetupStamperDragger" or funcNameOrFunc == t.SetupStamperDragger then
			return "Function SetupStamperDragger. Side Effect: Creates 4x4 stamping mechanism for building out parts quickly. Arguments: ModelToStamp, Mouse, LegalStampCheckFunction. ModelToStamp should be a Model or Part, preferrably loaded from RbxStamper.GetStampModel and should have extents that are multiples of 4.  Mouse should be a mouse object (obtained from things such as Tool.OnEquipped), used to drag parts around 'stamp' them out. LegalStampCheckFunction is optional, used as a callback with a table argument (table is full of instances about to be stamped). Function should return either true or false, false stopping the stamp action."
		end
	end

return t
