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
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
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local runService = game:GetService("RunService")
npc.Humanoid.WalkSpeed = 10
local bool = false
for _, part in npc:GetChildren() do
if part:IsA("Part") then
part.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if player.Character and not bool then
bool = true
player.Character.Humanoid:TakeDamage(10)
wait(1)
bool = false
end
end
end)
end
end
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)