My script is following the recently created character instead of the nearest
I would like to resolve this problem anytime
The Script:
local RunService = game:GetService("RunService") local OnlyPlayers = true local DebugMode = false local VelocityInhertance = false function checkSight(target,targetPart,Root) local ray = Ray.new(Root.Position, (targetPart.Position - Root.Position).Unit * 40) local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent,target}) if hit and hit.Parent and not hit.Parent:FindFirstChild("Humanoid") then if hit:IsDescendantOf(target.Parent) or Root.Position.Y < targetPart.Position.Y then return true end end return false end function Visual(Destination) local Hum = script.Parent.Humanoid local Path = PathfindingService:CreatePath() Path:ComputeAsync(Hum.RootPart.Position, Destination.Position) local Waypoints = Path:GetWaypoints() end function PathFind(Model,Destination) local Hum = script.Parent.Humanoid local Break = false local Path = PathfindingService:CreatePath() Path:ComputeAsync(Hum.RootPart.Position, Destination.Position) local Waypoints = Path:GetWaypoints() Path.Blocked:Connect(function(i) local w = Waypoints[i] if w and Hum then Hum.Jump = true Break = true end end) local LastPoint = Destination if Path.Status == Enum.PathStatus.Success then for i, waypoint in pairs(Waypoints) do if waypoint and Hum and Hum.RootPart and Destination then local LastWaypoint = Waypoints[i+1] or waypoint if DebugMode == true then local part = Instance.new("Part") part.Shape = "Ball" part.Material = "Neon" part.Size = Vector3.new(0.6, 0.6, 0.6) part.Position = waypoint.Position part.Anchored = true part.CanCollide = false part.Parent = game.Workspace game.Debris:AddItem(part,1) if Hum.RootPart.Position.Y < waypoint.Position.Y+1 then part.BrickColor = BrickColor.new("Sage green") else if LastWaypoint.Action == Enum.PathWaypointAction.Jump then part.BrickColor = BrickColor.new("Neon orange") elseif waypoint.Action == Enum.PathWaypointAction.Jump then part.BrickColor = BrickColor.new("Magenta") end end end if Hum.RootPart.Position.Y < waypoint.Position.Y+1 then Hum.Jump = true else if LastWaypoint.Action == Enum.PathWaypointAction.Jump then Hum.Jump = true elseif waypoint.Action == Enum.PathWaypointAction.Jump then Hum.Jump = true end end LastPoint = Waypoints[#Waypoints] Hum:MoveTo(waypoint.Position) if checkSight(Model,Destination,Hum.RootPart) == false or Break == true then Break = false break end LastPoint = waypoint.Position local Timeout = 0 repeat Hum:MoveTo(waypoint.Position) Timeout = Timeout + 1 if Hum.RootPart.Position.Y < waypoint.Position.Y then Hum.Jump = true else if LastWaypoint.Action == Enum.PathWaypointAction.Jump then Hum.Jump = true elseif waypoint.Action == Enum.PathWaypointAction.Jump then Hum.Jump = true end end if Hum.RootPart.Velocity.Magnitude < 0.1 or ((Hum.RootPart.Position-waypoint.Position)*Vector3.new(1,1,1)).Magnitude > 20 then PathFind(Destination,Hum.RootPart) break end RunService.Heartbeat:Wait() until ((waypoint.Position-Hum.RootPart.Position)*Vector3.new(1,0,1)).Magnitude < (2+(1+(Timeout/100))) do end end end elseif Destination and Hum and Hum.RootPart then Hum:MoveTo(LastPoint.Position) end end function GetNearestHum(Pos) local list = game.Workspace:children() local torso = nil local dist = 10000 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("Head") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and game.Players:FindFirstChild(temp2.Name) then if (temp.Position - Pos).magnitude < dist then torso = temp dist = (temp.Position - Pos).magnitude end end end end return human,dist end script.Parent.Humanoid.Touched:Connect(function(hit,bp) if hit and hit.Parent:FindFirstChild("Humanoid") then hit.Parent:FindFirstChild("Humanoid").Health = 0 end end) while RunService.Heartbeat:Wait() do if script.Parent:FindFirstChild("Humanoid") and script.Parent:FindFirstChild("Humanoid").RootPart then local hit,endp = workspace:FindPartOnRay(Ray.new(script.Parent:FindFirstChild("Humanoid").RootPart.Position + script.Parent:FindFirstChild("Humanoid").RootPart.CFrame.LookVector*2,script.Parent:FindFirstChild("Humanoid").RootPart.CFrame.LookVector*2 + Vector3.new(0,-8,0)),script.Parent) local NearHum,Dist = GetNearestHum(script.Parent.Humanoid.RootPart.Position) if NearHum and NearHum.Parent and NearHum.RootPart then if checkSight(NearHum.Parent, NearHum.RootPart, script.Parent.Humanoid.RootPart) == true then PathFind(NearHum.Parent, NearHum.RootPart) else if VelocityInhertance == true then script.Parent.Humanoid:MoveTo(NearHum.RootPart.Position+(NearHum.RootPart.Velocity*script.Parent.Humanoid.RootPart.CFrame.RightVector)) else --script.Parent.Humanoid:MoveTo(NearHum.RootPart.Position) script.Parent.Humanoid:MoveTo(script.Parent:FindFirstChild("Humanoid").RootPart.Position) end end if script.Parent.Humanoid.RootPart.Velocity.Magnitude < 0.1 or hit == nil then script.Parent.Humanoid.Jump = true end end end end