CollectionService:GetInstanceAddedSignal not firing inside OOP functions

I’m trying to make it so when a new tag is added is to an object and its parented to the workspace, the rbxsignal fires. If I put it in any of the functions, it doesn’t fire, but if its outside the functions, it does. any clue why?

function TempleNPCManager.new(tycoon)
	local self = setmetatable({}, TempleNPCManager)
	self.Tycoon = tycoon
	self._Maid = MaidService.new()
	
	-- doesn't fire
	self.NewAddedConnection = CollectionService:GetInstanceAddedSignal("TempleNPC"):Connect(function(part)
		print("new spawn added")
	end)
	
	return self
end
...

-- fires
CollectionService:GetInstanceAddedSignal("TempleNPC"):Connect(function(part:Instance)
	print("new spawn added")
end)

return TempleNPCManager

Try disabling/commenting out your MaidService and see if the problem goes away.

1 Like

Maid is not the issue it looks like, still not firing. I’ve copied it to outside the function, it works. Copy it back inside the class function, doesn’t fire

Make sure you add the tag after creating the rbxsignal? Tried using your code myself and it seems to work the same regardless of whether it’s in a function or not.

1 Like

Are you sure the function even fires at all? Possibly check with a print statement. If you’re getting a return on your TempleNPCManager self object, make sure the .NewAddedConnection index exists.

print(self.NewAddedConnection)
function TempleNPCManager.new(tycoon)
	
	print("making new TempleNPCManager")
	
	local self = setmetatable({}, TempleNPCManager)
	self.Tycoon = tycoon
	--self._Maid = MaidService.new()
	
	-- doesn't fire
	self.NewAddedConnection = CollectionService:GetInstanceAddedSignal("TempleNPC"):Connect(function(part)
		print("new spawn added")
	end)
	
	return self
end

console output
  21:59:51.925  making new TempleNPCManager  -  Server - TempleNPCManager:33
  21:59:51.925  Connection  -  Server - Tycoon:59 -- this is doing self.NewAddedConnection
line 56-59

	self.TempleNPCManager = TempleNPCManager.new(self)
	self.TempleNPCManager:Init() -- empty atm
	
	print(self.TempleNPCManager.NewAddedConnection)

Yea ok it fires when I add a new one, but if I load the tycoon with them preexisting, it doesn’t for some reason. Only works when I load tycoon, switch to server, and duplicate one. Not sure why, because if I run it outside the function, I don’t need to.

Oh! You have the solution, but I will add to it.
You can completely eliminate the race condition by having the system apply the function to pre-existing instances before binding an event.

This is how I would guarantee function firing to all of the tags.

local function tagFunction(tagObject)
	print("new spawn added")
end

for _,v in pairs(CollectionService:GetTagged("TempleNPC")) do
	task.defer(tagFunction, v) -- currently existing objects do not get picked up in the event signal, we catch it here instead
end
self.NewAddedConnection = CollectionService:GetInstanceAddedSignal("TempleNPC"):Connect(tagFunction)
1 Like

Seems to be the best option. Thank you so much for your time, I appreciate it.

1 Like

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