-- the following data is to update the ObjectTable

local ValidClassNames = {"Part","WedgePart","TrussPart","Model","Seat","VehicleSeat"}
function isValidClass(part)
	local CN = part.className 
	for i=1,#ValidClassNames do
		if(CN == ValidClassNames[i]) then
			return true
		end
	end
	return false
end
function isValidPart(part)	-- returns className if its a part or model otherwise returns nil
	if isValidClass(part) == true then
		return part
	end
   return nil -- not a part or an model
end

function retriveParts(model) -- main function for retriving all the parts of a model and loading them into a table
	if(isValidPart(model) and model.className ~= "Model") then
			WorkSpaceObjects[WSONum] = model;
			WSONum = WSONum + 1;
	else
		local raw_model = model:GetChildren()
		for i=1,#raw_model do
			local part = isValidPart(raw_model[i])
			if(part ~= nil) then
				if(part.className == "Model") then
					retriveParts(part);
				else
					if(part.Anchored == false) then
						--if(part:findFirstChild("TAG") == nil) then
						--	Tag = Instance.new("ObjectValue")
						--	Tag.Parent = part
						--	Tag.Name = "TAG"
							WorkSpaceObjects[WSONum] = part;
							WSONum = WSONum + 1;
						--end
					end
				end
			end
		end
	end
end
function RemoveBits(model) -- Function for removing bits from our global table as they dont exist in the workspace anymore
	if(isValidPart(model) and model.className ~= "Model") then
		for i=1,#WorkSpaceObjects	do
			if(model == WorkSpaceObjects[i]) then
				table.remove(WorkSpaceObjects,i)
				WSONum = WSONum -1
			end
		end
	else
		local raw_model = model:GetChildren()
		for i=1,#raw_model do
			local part = raw_model[i]
			if(part.className == "Model") then
				RemoveBits(part);
			else
				for i=1,#WorkSpaceObjects	do
					if(part == WorkSpaceObjects[i]) then
						table.remove(WorkSpaceObjects,i)
						WSONum = WSONum -1
					end
				end
			end
		end
	end
end

retriveParts(game.Workspace); -- call our retreiver
game.Workspace.ChildAdded:connect(retriveParts)-- just encase any new objects get added to the game
game.Workspace.ChildRemoved:connect(RemoveBits)