local E=Vector3.new(3,2.8,3)local R=script.Parent local U=R.Contents local A=3 function r(E)return math.random(-math.abs(E*100),math.abs(E*100))/100 end local function J(E,R)local U=Instance.new("\077\097\110\117\097\108\087\101\108\100",E)U.Part0=E U.Part1=R U.C0=E.CFrame:inverse()*R.CFrame return U end R.Base.Touched:connect(function(A)if A:FindFirstChild("\068\114\097\103\103\097\098\108\101")and A:FindFirstChild("\080\105\099\107\117\112")or A.Parent and(A.Parent:FindFirstChild("\068\114\097\103\103\097\098\108\101")and A.Parent:FindFirstChild("\080\105\099\107\117\112"))then local g if A.Parent:IsA("\077\111\100\101\108")and A.Parent~=workspace.Items then g=A.Parent:Clone()A.Parent:Destroy()elseif A.Parent==workspace.Items then g=A:Clone()A:Destroy()end if g.Draggable then g.Draggable:Destroy()end if g:IsA("\066\097\115\101\080\097\114\116")then g.Anchored=false g.CanCollide=false local U=E-g.Size print(U,"\097\115 \118\097\114\105\097\110\099\101")g.CFrame=R.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100)J(g,R.PrimaryPart)elseif g:IsA("\077\111\100\101\108")then for E,R in next,g:GetDescendants()do if R:IsA("\066\097\115\101\080\097\114\116")then R.Anchored=false R.CanCollide=false if R==g.PrimaryPart then continue end J(R,g.PrimaryPart)end end local U=g:GetExtentsSize()local A=E-U g:SetPrimaryPartCFrame(R.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100))J(g.PrimaryPart,R.PrimaryPart)end g.Parent=U end end)

local freeSpace = Vector3.new(3,2.8,3)
local box = script.Parent
local contents = box.Contents

local lidOffset = 3 

function r(num)
	return math.random(-math.abs(num*100),math.abs(num*100))/100
end

local function weldBetween(a, b)
	--Make a new Weld and Parent it to a.
	local weld = Instance.new("ManualWeld", a)
	weld.Part0 = a
	weld.Part1 = b
	--Get the CFrame of b relative to a.
	weld.C0 = a.CFrame:inverse() * b.CFrame
	--Return the reference to the weld so that you can change it later.
	return weld
end

box.Base.Touched:connect(function(oldHit)

	if (oldHit:FindFirstChild("Draggable") and oldHit:FindFirstChild("Pickup")) or
		(oldHit.Parent and (oldHit.Parent:FindFirstChild("Draggable") and oldHit.Parent:FindFirstChild("Pickup")))
	then
		local hit
		if oldHit.Parent:IsA("Model") and oldHit.Parent ~= workspace.Items then
			hit = oldHit.Parent:Clone()
			oldHit.Parent:Destroy()
		elseif oldHit.Parent == workspace.Items then
			hit = oldHit:Clone()
			oldHit:Destroy()
		end

		if hit.Draggable then
			hit.Draggable:Destroy()
		end

		if hit:IsA("BasePart") then
			hit.Anchored = false
			hit.CanCollide = false
			local vary = freeSpace-hit.Size
			-- random x,y,z
			print(vary,"as variance")
			--hit.CFrame = box.PrimaryPart.CFrame*CFrame.new(r(vary.X),1+math.random(0,vary.Y*100)/100,r(vary.Z))
			hit.CFrame = box.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100)
			weldBetween(hit, box.PrimaryPart)
		elseif hit:IsA("Model") then
			for _,v in next,hit:GetDescendants() do
				if v:IsA("BasePart") then
					v.Anchored = false
					v.CanCollide = false
					if v == hit.PrimaryPart then continue end
					weldBetween(v, hit.PrimaryPart)
				end
			end
			local modelSize=  hit:GetExtentsSize()
			local vary = freeSpace-modelSize
			--hit:SetPrimaryPartCFrame(box.PrimaryPart.CFrame*CFrame.new(r(vary.X),1+math.random(0,vary.Y*100)/100,r(vary.Z)))
			hit:SetPrimaryPartCFrame(box.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100))
			weldBetween(hit.PrimaryPart, box.PrimaryPart)
		end -- end of if hit is a basepart or model
		hit.Parent = contents
	end
end)
