GetTagged is very unreliable

I’m trying to load in required modules using GetTagged but it never gets the instances unless I re-assign them the tags, why?

task.spawn(function()
	for _,v in CollectionService:GetTagged("Required") do
		if not v:GetAttribute("Server") then
			print(v:GetFullName())
			return
		end
		print(v:GetFullName())
		local Successful, Warning = pcall(require(v).Run)
		if not Successful then
			warn(v:GetFullName(), "failed! server", Warning)
		end
	end
end)

image

Hi! I am a bit unfamiliar with the method you are trying to use, as well as its intended function but one thing to note is that loading in Roblox is much different from Roblox Studio. In Roblox, different components will take x amount of time to load. This means that sometimes it loads pretty fast, other times it will take a bit. What I suspect is happening here is that even though you wait for the script to load (hence your task.spawn) the modules may load in different times (I might be misinterpreting this, I am not sure). The best way I would go about is waiting for x amount of time, before running the function, or load all the things first, then run the script. However, could your intent in the code you provided? Perhaps there might be an alternate way than use tags?

This could be it but I don’t really wanna use GetTaggedInstanceAdded signal since it may slow.

Doubt that since the modules usually are just this.

FFModule.Run = function()
	print(script.Name, "Is running!")
	for _,v in CollectionService:GetTagged("ForceField") do
		Init(v)
	end
end

Oh ok, how about just using remotes instead, since it would help synchronize the timings as well as it should fire to all listeners? I feel like using a remote-function model might be more reliable to get the task conveyed in both a succinct and orderly manner. Additionally, if it’s in a module script, simply requiring it then inputting the listener should do the respective task defined.

I’m quite confused on what you meant by remotes? The modules are for one contexts only, like server-to-server and client-to-client, hence the attribute.

Maybe it would help if we could see the innards of the module? Maybe I’m not understanding something, but seeing what the modules is providing for those values may shed some light on the issue.

I apologize for my confusing wording, by remotes I was assuming that you are trying to activate specific portions of module scripts. So I suggested using bindable events so you can maintain server to server and client to client communication as needed.

1 Like

Here’s a version with :GetInstanceAddedSignal.

Code:

task.spawn(function()
	local function onRequiredAdded(v)
		if not v:GetAttribute("Server") then
			return
		end
		
		local success, errorMessage = pcall(require(v).Run)
		
		if not success then
			warn(v:GetFullName(), "failed! server", errorMessage)
		end
	end
	
	CollectionService:GetInstanceAddedSignal("Required"):Connect(onRequiredAdded)
	for _, v in CollectionService:GetTagged("Required") do
		onRequiredAdded(v)
	end
end)
2 Likes

I agree with RealCalculus here; while it doesn’t solve that specific issue, it does provide a simpler alternative means that allows you to avoid using that event you mentioned. In my unprofessional opinion I believe it has something to do with that script executing while those instances’ ‘tags’ are loading in, causing them to not be read unless you re-add them to the modules. Just speculation though. Maybe you could even task.wait() and achieve the desired result?

You could, but that doesn’t solve the main issue of this script running before all the objects are loaded. What if the OP wants to add new modules during runtime? Or the scripts take longer to load than the time you’re waiting for?

1 Like

All modules either have Touched or interactions with the basepart being tagged. Plus even if it errored pcall would’ve catched it.

I feel like you could

repeat task.wait(0.2) until ____ exists

Yeah, that’s less efficient and slower than :GetInstancesAddedSignal.

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