local C=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local J=require(C.Shared.components)local E=require(C.Lib.Matter)local function l(C)for l,f in C:query(J.Model)do for E in E.useEvent(f.model,"\065\110\099\101\115\116\114\121\067\104\097\110\103\101\100")do if f.model:IsDescendantOf(game)==false then C:remove(l,J.Model)break end end if not f.model.PrimaryPart then C:remove(l,J.Model)end end for C,J in C:queryChanged(J.Model)do if J.new==nil then if J.old and J.old.model then J.old.model:Destroy()end end end end return l
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local Matter = require(ReplicatedStorage.Lib.Matter)

local function removeMissingModels(world)
	for id, model in world:query(Components.Model) do
		for _ in Matter.useEvent(model.model, "AncestryChanged") do
			if model.model:IsDescendantOf(game) == false then
				world:remove(id, Components.Model)
				break
			end
		end
		if not model.model.PrimaryPart then
			world:remove(id, Components.Model)
		end
	end

	for _id, modelRecord in world:queryChanged(Components.Model) do
		if modelRecord.new == nil then
			if modelRecord.old and modelRecord.old.model then
				modelRecord.old.model:Destroy()
			end
		end
	end
end

return removeMissingModels
