Zombie NPC help

STILL NEED 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

Nothing happened … ): … i pasted it

Now my zombies stopped moving completely, they are just spawning, any reason for this?

1 Like

set the script enabled property to false and after spawning the zombie and parenting it to the workspace enable the script

Here ya go:

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)
1 Like

God bless you omg you saved my game :brown_heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.