I copied this script from a youtuber and it doesnt work, does anyone now why? There are no errors and the enemy doesnt move.

local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}

local maxDistance = math.huge


npc.Humanoid.Touched:Connect(function(touch)

	if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then

		plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true

		touch.Parent.Humanoid:TakeDamage(20)

		wait(1)

		plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = false  
	end
end)


while wait() do

	local plrs = game.Players:GetPlayers()

	local closestHRP

	for i, plr in pairs(plrs) do

		if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then

			local hrp = plr.Character.HumanoidRootPart

			local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude


			if not closestHRP then closestHRP = hrp end

			if (hrpOfNPC.Position - closestHRP.Position).Magnitude < distanceBetween then

				closestHRP = hrp

			end
		end
	end

	if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then npc.Humanoid:MoveTo(closestHRP.Position) end
end

4 comments

0

Who did you get this off of? It's very inefficient.

0

HowToRoblox.

0

hes a guy on Youtube.

0

@Ziffixture also if youcan answer please do! I dont know other people who can help me