CollectionService doesn't find every tagged instance

Hello, I’m making an area unlock system. I’m using CollectionService to access every area barrier, though CollectionService doesn’t seem to recognize every tagged barrier?

ModuleScript in ReplicatedStorage:

local areaModule = {
	["Beach"] = {
		["Currency"] = "Coins";
		["Price"] = 10000;
		["DeveloperProduct"] = 1599214249;
	};
	
	["Snowy Forest"] = {
		["Currency"] = "Coins";
		["Price"] = 50000;
		["DeveloperProduct"] = 1605133193;
	};
	
	["Volcano"] = {
		["Currency"] = "Coins";
		["Price"] = 250000;
		["DeveloperProduct"] = 1634720922;
	};
	
	["Blossoms"] = {
		["Currency"] = "Coins";
		["Price"] = 1000000;
		["DeveloperProduct"] = 1634722134;
	};
	
	["Forest"] = {
		["Currency"] = "Coins";
		["Price"] = 5000000;
		["DeveloperProduct"] = 1634722294;
	};
	
	["Farm"] = {
		["Currency"] = "Coins";
		["Price"] = 25000000;
		["DeveloperProduct"] = 1634722410;
	};
	
	["Cave"] = {
		["Currency"] = "Coins";
		["Price"] = 100000000;
		["DeveloperProduct"] = 1634722525;
	};
}

return areaModule

LocalScript in StarterPlayerScripts:

local collectionService: CollectionService = game:GetService("CollectionService")

local areaModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("AreaModule"))

local areas = {}

for i, area in areaModule do
	table.insert(areas, i)
end

local areasToFind: number = #areas

local function getAreas()
	print(#collectionService:GetTagged("Area Barriers"))

	for _, areaBarrier in collectionService:GetTagged("Area Barriers") do
		if areaBarrier:GetAttribute("AreaName") and table.find(areas, areaBarrier:GetAttribute("AreaName")) then
			areasToFind -= 1
		end
	end
end

collectionService.TagAdded:Connect(function(tag: string)
	if tag ~= "Area Barriers" then return end
	
	while true do
		getAreas()
		task.wait(1)
	end
end)

print(#collectionService:GetTagged("Area Barriers")) only returns 4, while there are 7 barriers.

I think this might be related to streaming. The server should have all 7 of them, but the client at any given time would only have the ones that they’re close to / that their machine has streamed in.

The solution might be to change it to mix GetTagged and GetInstanceAddedSignal instead. You’d set it up with the original GetTagged for anything that starts off in the game for the client (sounds like there’s the four of them), and then if any others get added in later, you’d re-run the same code on the new ones.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.