Using collectionservice to change tagged NPC health to 1 only works once

  1. What do you want to achieve? to have every NPC tagged “Zombie” health set to “1”

  2. What is the issue? The zombies spawned in will have their health set to 1, but if new zombies spawn in they’ll reset back to their health

  3. What solutions have you tried so far? tried adding a while do true, but doing that made the script not work with no errors in output

local CS = game:GetService("CollectionService")
local temp = CS:GetTagged("Zombie")

	for i, v in pairs(temp) do
		if v:IsA("Model") then
			local Hum = v:FindFirstChild("Humanoid")
			if Hum then
				
			Hum.Health = 1

		else
print "nil"
			end
		end
		end


	

You aren’t using something that will keep the script running.
You were originally correct, but while true do normally exhausts the script:

local CS = game:GetService("CollectionService")
local temp = CS:GetTagged("Zombie")
while wait() do
	for i, v in pairs(temp) do
		if v:IsA("Model") then
			local Hum = v:FindFirstChild("Humanoid")
			if Hum then
			    Hum.Health = 1
            end
		else
            print "nil"
		end
	end
end
1 Like

Just make one zombie and clone it
and on that Zombie set the health to 1
and there’s no way it’ll change except it somehow has a healing factor like players

1 Like

so i tried this, but it shows the same results. when the next round starts the zombies health are reset back to their regular health value. even though adding in the “while wait() do” statement it should be constantly looping correct? so if a NPC spawned in and tagged “Zombie” it should have it health set back to 1?

1 Like

Yes, it should always be looping. A NPC tagged “Zombie”, from what I figured out, should have its health set to 1.

1 Like

where do the Zombies come from as long as they are not new Instances the values can be set before the game starts even if they respawn as long as the zombies are not players themselves

1 Like

Zombies come from ReplicatedStorage

1 Like

This might fix it:

if v:IsA("Model") then
	local Hum = v:FindFirstChild("Humanoid")
	if Hum then
	    Hum.Health = 1
            Hum.MaxHealth = 1
	else
        print "nil"
	end
end
1 Like

Thats what im thinking too. not sure why its working. im looking through other scripts to see if anything could be interfering with it. im thinking when a new round starts the health value is changed by “* 1.1” or " + 100", but then again once the script loops it should set it back to 1. Hmm…

1 Like

Have you tried using print statements to see if it is running the code?

1 Like

You only call collectionService:GetTagged(tag) once, it doesn’t update automatically, so you need to call it each frame in order to also get any newly added zombie objects.

I’d recommend listening for when new instances get added with the zombie tag using the GetInstanceAddedSignal function

local function onZombieAdded(zombie)
zombie.Humanoid.Health = 1
end

collectionService:GetInstanceAddedSignal("Zombie"):Connect(onZombieAdded)

--//Get already existing zombies
for _, zombie in ipairs(collectionService:GetTagged("Zombie")) do
onZombieAdded(zombie)
end
2 Likes

what is the value of the Zombies health in Replicated Storage

1 Like

this seemed to fix the issue! thank you guys for helping me out :grinning: also thank you for explaining the solution, i had no idea collectionService only calls once.

2 Likes