For some reason every time I try to call a function on a tagged mob to check for the humanoid’s health, it keeps saying “attempt to index nil with Humanoid”.
I’ve already done some tests and I can safely say that the “Mob” does exist and it does have a parent and it is an instance.
So I have no idea why this isn’t working, but it definitely feels like I’m overlooking something
local TaggedMobs = CollectionService:GetTagged("Ghoul")
--Function--
local function DecideAction(Mob)
if Mob.Humanoid.Health <= 0 then
print("Dead")
--Code
end
end
end
--Main--
for Index, TargetMob in pairs(TaggedMobs) do
DecideAction(TargetMob)
end
Please let me know if you need any more information, thanks.
looks really odd, are you inserting values into the table as well by any chance? This shouldn’t happen as collection services GetTagged should either return an Instance that has been tagged or not so it can’t be nil.
When in doubt print
for Index, TargetMob in pairs(TaggedMobs) do
print(Index,TargetMob)
end
Somehow, TargetMob is becoming nil within the table while there is still an instance in it.
I haven’t touched the table at all, but yea It’s really weird, after a bit of messing around it, I found out that yielding the script for a second fixes it. Although I still have some problems after the mob finishes their attack cycle, however that’s most likely an implementation error on my part.