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
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)