This is a script, located in ServerScriptService. The issue I am having is the nothing happening to the NPCs, but it should.
This is the script I am using.
local ZombiesFolder = game:GetService("Workspace").Map:WaitForChild("Zombies")
for _, zombie in pairs(ZombiesFolder:GetChildren()) do
task.spawn(function()
local Humanoid = zombie:WaitForChild("Zombie")
local function Died()
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats ~= nil then
Leaderstats.Cash.Value = Leaderstats.Cash.Value + math.random(7, 12)
Leaderstats.Kills.Value = Leaderstats.Kills.Value + 1
Leaderstats.Exp.Value = Leaderstats.Exp.Value + math.random(11, 17)
wait(0.1)
script:remove()
end
end
end
end
local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
local function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 600
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while task.wait(1) do
Humanoid.Died:connect(Died)
while true do
wait(1)
local target = findNearestTorso(zombie.HumanoidRootPart.Position)
if target ~= nil then
Humanoid:MoveTo(target.Position, target)
end
end
end
end)
end
What this script is supposed to be doing is moving the NPC to the nearest torso (player) and this script also handles the NPC respawn as well.