How can CollectionService find a certain child with the correct tag?

Hello, so in my Tower Defense Game I am trying to make a tower that slows down the mob they attack. It worked when I made and pasted the same script to every mob model in the folder, but I wanted to use Collection Service to optimize it. So when I try to do that, it doesn’t work anymore. Can anyone help me? This is the script I wrote and I’m not understanding why it is not working. (The Slowed bool change works but the problem is that they don’t get the effect to work.)

for _, mob in pairs(CollectionService:GetTagged("Mobs")) do
	-- // SLOW EFFECT
	mob:WaitForChild("StunEffects").Slowed.Changed:Connect(function(value)
		if value then
			mob.Humanoid.WalkSpeed = 1.8
			repeat
				mob:WaitForChild("StunEffects").SlowedDuration.Value += 1
				task.wait(1)
			until mob:WaitForChild("StunEffects").SlowedDuration.Value >= 5 or mob.Humanoid.Health <= 0
			mob.Humanoid.WalkSpeed = mob.Config.Speed.Value
			mob:WaitForChild("StunEffects").SlowedDuration.Value = 0
			mob:WaitForChild("StunEffects").Slowed.Value = false 
		else
			mob:WaitForChild("StunEffects").SlowedDuration.Value = 0
			mob:WaitForChild("StunEffects").Slowed.Value = false
			mob.Humanoid.WalkSpeed = mob.Config.Speed.Value
		end	
	end)
end
1 Like

Are you sure you set the tags to every model?

Yea, every NPC in the Mobs folder have the Mobs tag. I used the plugin and yes, its correct.

Try this:

local function InitializeMob(mob)
	print(mob.Name, "initialized.")
	
	local StunEffects = mob:WaitForChild("StunEffects")
	
	StunEffects.Slowed.Changed:Connect(function(value)
		if value then
			mob.Humanoid.WalkSpeed = 1.8
			
			repeat
				StunEffects.SlowedDuration.Value += 1
				
				task.wait(1)
			until StunEffects.SlowedDuration.Value >= 5 or mob.Humanoid.Health <= 0
			
			mob.Humanoid.WalkSpeed = mob.Config.Speed.Value
			StunEffects.SlowedDuration.Value = 0
			StunEffects.Slowed.Value = false 
		else
			StunEffects.SlowedDuration.Value = 0
			StunEffects.Slowed.Value = false
			mob.Humanoid.WalkSpeed = mob.Config.Speed.Value
		end	
	end)
end

for i, mob in ipairs(CollectionService:GetTagged("Mobs")) do
	InitializeMob(mob)
end

CollectionService:GetInstanceAddedSignal("Mobs"):Connect(function(mob)
	InitializeMob(mob)
end)

Tell me if it prints anything.

Sorry for the late response, also yes! It prints the name of every mob and it works now! But this is what I got, is it a problem?

I’ve also been getting that error in every game. It’s a Roblox problem we can’t solve. It will probably be fixed later today.

If my solution worked, please set it as the solution.

Oh alright. Well yes, roblox is having issues right now. However, thank you for the help :slight_smile:

No problem. If you have any more questions, feel free to ask.

1 Like