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