I am trying to make an Jeep, to drive though its path, and kill zombies on collision with them using its own HP. In my case, I get error that Humanoid doesn’t exist. How I can do this: JeepHP = JeepHP - ZombieHP and make zombie also get damage by JeepHP. How I can achieve this?
My script that does that part:
function findPossibleTarget()
for _, v in pairs(Enemy:GetChildren()) do
if v:FindFirstChild("Torso") then
if Model.PrimaryPart then
if (Model.PrimaryPart.Position - v.Torso.Position).magnitude < _Range.Value then
if v:FindFirstChild("Humanoid") then
if v.Humanoid then
v.Humanoid.Health = (Humanoid.Health - v.Humanoid.Health)
if Humanoid then
Humanoid.Health = Humanoid.Health - v.Humanoid.Health
end
end
end
end
end
end
end
end
Error I get:
19:21:44.650 - Humanoid is not a valid member of Model 19:21:44.650 - Stack Begin 19:21:44.651 - Script 'Workspace.Jeep.CoreScript', Line 60 - global findPossibleTarget 19:21:44.651 - Script 'Workspace.Jeep.CoreScript', Line 73 - global onUpdate 19:21:44.651 - Script 'Workspace.Jeep.CoreScript', Line 46 19:21:44.651 - Stack End
You can’t do if v.Humanoid then. It will give an error. Replace it with if v:FindFirstChild(“Humanoid”) then and you should be good to go. EDIT: Didn’t see line above so I’m not sure if what I said will fix it or not. In future doing checking something like that won’t work if it doesn’t exist.
I was working a bit, this doesn’t give error but doesn’t work
function findPossibleTarget()
for _, v in pairs(Enemy:GetChildren()) do
if v:FindFirstChild("Torso") then
if Model.PrimaryPart then
if (Model.PrimaryPart.Position - v.Torso.Position).magnitude < _Range.Value then
if v:FindFirstChild("Humanoid") then
if v["Humanoid"] and Humanoid then
local ZombieHP = v.Humanoid.Health
local TowerHP = Humanoid.Health
v["Humanoid"]:TakeDamage((ZombieHP - TowerHP))
Humanoid:TakeDamage((TowerHP - ZombieHP))
end
end
end
end
end
end
end
Takedemage is decreasing hp and when jeep have 1000 hp and zombie 100 you will decrease -900 hp, so you will heal him, so only decrese it by truck.
Takedemage will do inside
Hp=Hp-parametr1