local k=Vector3.new(3,2.8,3)local q=script.Parent local P=q.Contents local L=3 function r(k)return math.random(-math.abs(k*100),math.abs(k*100))/100 end local function b(k,q)local P=Instance.new("\077\097\110\117\097\108\087\101\108\100",k)P.Part0=k P.Part1=q P.C0=k.CFrame:inverse()*q.CFrame return P end q.Base.Touched:connect(function(L)if L:FindFirstChild("\068\114\097\103\103\097\098\108\101")and L:FindFirstChild("\080\105\099\107\117\112")or L.Parent and(L.Parent:FindFirstChild("\068\114\097\103\103\097\098\108\101")and L.Parent:FindFirstChild("\080\105\099\107\117\112"))then local w if L.Parent:IsA("\077\111\100\101\108")and L.Parent~=workspace.Items then w=L.Parent:Clone()L.Parent:Destroy()elseif L.Parent==workspace.Items then w=L:Clone()L:Destroy()end if w.Draggable then w.Draggable:Destroy()end if w:IsA("\066\097\115\101\080\097\114\116")then w.Anchored=true w.CanCollide=false local P=k-w.Size print(P,"\097\115 \118\097\114\105\097\110\099\101")b(w,q.PrimaryPart)w.CFrame=q.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100)elseif w:IsA("\077\111\100\101\108")then for k,P in next,w:GetDescendants()do if P:IsA("\066\097\115\101\080\097\114\116")then P.Anchored=true P.CanCollide=false b(P,q.PrimaryPart)end end local P=w:GetExtentsSize()local L=k-P w:SetPrimaryPartCFrame(q.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100))end w.Parent=P end end)
--local freeSpaceX,freeSpaceY,freeSpaceZ = 3,3,2.8
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 = true
			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))
			weldBetween(hit, box.PrimaryPart)
			hit.CFrame = box.PrimaryPart.CFrame
				* CFrame.new(math.random(-100, 100) / 100, math.random(100, 200) / 100, math.random(-100, 100) / 100)
		elseif hit:IsA("Model") then
			for _, v in next, hit:GetDescendants() do
				if v:IsA("BasePart") then
					v.Anchored = true
					v.CanCollide = false
					weldBetween(v, box.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
					)
			)
		end -- end of if hit is a basepart or model
		hit.Parent = contents
	end
end)
