NPC Script not working

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.

2 Likes

I didn’t check the script but I think you need to make the npc to r6 not r15 if it is r15 or rthro

The NPC is R15 and it has nothing to do with R6 or R15.

yeah maybe but if you want to play for example an animation it will only work r6

Check the script. As I said, it moves the HumanoidRootPart of the NPC to the nearest Player Torso, so it have nothing to do with it being animated either.

k i’ll see it sorry for making u angry (if I did) lol

All good, lol. *******************

I didn’t check it fully yet but you added script:remove which removes the script

btw did you check any error it will help you where the error is

Can’t believe I missed that all along, let me go ahead and check.

Removed it and nope, it still doesn’t work.

No. **********************************************

I couldn’t find the error but I recommend again to check if any error comes out

1 Like