Problem in Player Chasing

Hi Developers , I created this script and it worked great but when I repeated this script with another NPC’s of the same type more than once it didn’t work if you have experience please help me
Note: I used zoneplus and created a variable inside the script to hold the name of the player

local Humanoid = script.Parent:WaitForChild("Humanoid")
local hpart = script.Parent:WaitForChild("HumanoidRootPart")
local name = script.name
local inregoin = false
local npcisout = false
local first = hpart.Position
local zone = require(game.ReplicatedStorage:WaitForChild("Zone"))
local myzone = zone.new(game.Workspace:WaitForChild("zone1"))
myzone.playerEntered:Connect(function(player)
	inregoin = true
	script:WaitForChild("name").Value = player.Name
end)
myzone.playerExited:Connect(function(player)
	if script.Parent then
		Humanoid.Parent:MoveTo(first)
	else

	end
end)
while true do 
	wait(0.1)
	if inregoin == true then
		if name.Value ~= "None" then
			for _,v in pairs(game.Players:GetChildren()) do
				local char = game.Workspace:WaitForChild(v.Name)
				local head = Humanoid.Parent:WaitForChild("Head")
				Humanoid:MoveTo(char:WaitForChild("HumanoidRootPart").Position)
				local des = (hpart.Position-char:WaitForChild("HumanoidRootPart").Position).magnitude
				if des < 30 then
					if inregoin == true then
						if npcisout == false then
							Humanoid:MoveTo(char:WaitForChild("HumanoidRootPart").Position)
						end
					end
				end
			end
		end
	end
end
1 Like