--!strict


local Cart = script.Parent.Parent
local Base = Cart:WaitForChild("Base")

local BreakSound = Base:WaitForChild("Break")

local Seat = Cart:WaitForChild("Seat")

return function()
	local occupant = Seat.Occupant
	if occupant then
		occupant.Sit = false
	end
	Seat.Disabled = true
	BreakSound:Play()
	if Base.AssemblyRootPart then
		Base.AssemblyRootPart:SetNetworkOwner(nil)
	end
	local descendants = Cart:GetDescendants()
	for _, descendant in ipairs(descendants) do
		if descendant:IsA("WeldConstraint") or descendant:IsA("Constraint") then
			descendant.Enabled = false
		end
	end
	for _, descendant in ipairs(descendants) do
		if descendant:IsA("BasePart") then
			descendant:ApplyImpulse(Vector3.new(0, math.random(70, 70), 0) * 0.5 * descendant.AssemblyMass)
			descendant:ApplyAngularImpulse(Vector3.new(math.random(-10, 10), math.random(-10, 10), math.random(-10, 10)) * 1 * descendant.AssemblyMass)
		end
	end
	task.spawn(function()
		for _, ch in ipairs(Cart:GetDescendants()) do
			if ch:IsA("BasePart") then
				task.delay(math.random() * 4, ch.Destroy, ch)
			end
		end
		task.wait(4)
		Cart:Destroy()
	end)
end
