Zombie NPC help

Hello!! i want to make my custom zombie NPC from scratch be able to damage players health little by little, Its already following the player its just that little detail that i want to add, I have tried chatgpt and youtube but surprisingly they didnt really help with something good. Please add a simple damage system to this existing script inside my dummy/zombie:

//////////////////////

local npc = script.Parent
local humanoid = npc:WaitForChild(“Humanoid”)
local runService = game:GetService(“RunService”)

npc.Humanoid.WalkSpeed = 10 – Adjust the speed here

runService.Heartbeat:Connect(function()
local nearestPlayer, shortestDistance
for _, player in pairs(game.Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild(“HumanoidRootPart”) then
local distance = (character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
if not shortestDistance or distance < shortestDistance then
nearestPlayer = character
shortestDistance = distance
end
end
end

if nearestPlayer then
	humanoid:MoveTo(nearestPlayer.HumanoidRootPart.Position)
end

end)

1 Like

what do u want to add? the damaging system?

a simple system that damages a player whenever the npc touches/is touching a player, As its a zombie NPC

local Zombie = workspace.Part1 -- The Zombie Model
local HitTable = {}

local DMG = 10
local Cooldown = 3

for i, v in pairs(Zombie:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(Hit)
			local Model = Hit:FindFirstAncestorOfClass("Model")
			
			if Model.Name ~= "Zombie" then --Checks if the model is not zombie
				local Humanoid = Model:FindFirstChild("Humanoid") -- checks if the model have life
				
				if HitTable[Humanoid] == nil then -- checks if the humanoid was not recently damaged
					HitTable[Humanoid] = true
					Humanoid.Health -= DMG -- takes damage
					
					task.delay(Cooldown, function() -- the humanoid can be damaged again after the cooldown
						HitTable[Humanoid] = nil
					end)
				end
				
			end
			
		end)
	end
end

i edited it again, i forgot to add the other ends