What do you want to achieve? to have every NPC tagged “Zombie” health set to “1”
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
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
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
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?
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
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…
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
this seemed to fix the issue! thank you guys for helping me out also thank you for explaining the solution, i had no idea collectionService only calls once.