local O=Vector3.new(3,2.8,3)local w=script.Parent local h=w.Contents local M=3 function r(O)return math.random(-math.abs(O*100),math.abs(O*100))/100 end w.Base.Touched:connect(function(M)local j if M:FindFirstChild("\068\114\097\103\103\105\110\103")then return end if M:FindFirstChild("\068\114\097\103\103\097\098\108\101")and M:FindFirstChild("\080\105\099\107\117\112")then j=M:Clone()M:Destroy()elseif M.Parent and(M.Parent:FindFirstChild("\068\114\097\103\103\097\098\108\101")and M.Parent:FindFirstChild("\080\105\099\107\117\112"))then j=M.Parent:Clone()M.Parent:Destroy()else return end for O,w in next,j:GetDescendants()do if w.Name=="\068\114\097\103\103\097\098\108\101"then w:Destroy()end end if j:IsA("\066\097\115\101\080\097\114\116")then j.Anchored=true j.CanCollide=false local h=O-j.Size print(h,"\097\115 \118\097\114\105\097\110\099\101")j.CFrame=w.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100)elseif j:IsA("\077\111\100\101\108")then for O,w in next,j:GetDescendants()do if w:IsA("\066\097\115\101\080\097\114\116")then w.Anchored=true w.CanCollide=false end end local h=j:GetExtentsSize()local M=O-h j:SetPrimaryPartCFrame(w.PrimaryPart.CFrame*CFrame.new(math.random(-100,100)/100,math.random(100,200)/100,math.random(-100,100)/100))end j.Parent=h 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

box.Base.Touched:connect(function(oldHit)
	
	local hit
	
	if oldHit:FindFirstChild("Dragging") then
		return
	end
	
	if (oldHit:FindFirstChild("Draggable") and oldHit:FindFirstChild("Pickup")) then
		hit = oldHit:Clone()
		oldHit:Destroy()
	elseif (oldHit.Parent and (oldHit.Parent:FindFirstChild("Draggable") and oldHit.Parent:FindFirstChild("Pickup"))) then
		hit = oldHit.Parent:Clone()
		oldHit.Parent:Destroy()
	else
		return
	end
	
	for _, child in next, hit:GetDescendants() do
		if child.Name == "Draggable" then
			child:Destroy()
		end
	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))
		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
			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)
