Hello Developers!
I want to modify this script so NPCs will defend from you
--Services-- local Players = game:GetService("Players"); local RunService = game:GetService("RunService"); local PathFindingService = game:GetService("PathfindingService"); --Varibles-- local Character = script.Parent; local Humanoid = Character.Humanoid; local RootPart = Character.HumanoidRootPart; local CanJump = true; local AttackRange = Vector3.new(3,3,3); local FieldOfView = 70; local CharacterSize = Character:GetExtentsSize(); local PathParams = { AgentRadius = (CharacterSize.X+CharacterSize.Z)/4, AgentHight = CharacterSize.Y, AgentCanJump = CanJump, }; --Functions-- local function GetNearestPlayer() local players = {}; local nearest = math.huge; local target = nil; for _, player in pairs(Players:GetPlayers()) do local character = player.Character; if character == nil then continue; end; local distance = (character.HumanoidRootPart.Position - RootPart.Position); if distance.Magnitude <= FieldOfView then table.insert(players,{ Magnitude = distance.Magnitude, Player = player, }); end; end; for _, entry in pairs(players) do local magnitude = entry.Magnitude; local Player = entry.Player; if magnitude < nearest then nearest = magnitude; target = Player; end; end; return target; end --Connections-- RunService.Heartbeat:Connect(function() local player = GetNearestPlayer(); local player_character = player.Character; if player_character == nil then return; end; local Character_Humnoid = player_character.Humanoid if Character_Humnoid.Health <= 0 then return; end; local Target_RootPart = player_character.HumanoidRootPart; local Destination = Target_RootPart.Position; local Beginning = RootPart.Position; local path = PathFindingService:CreatePath(PathParams); path:ComputeAsync(Beginning,Destination); if path.Status == Enum.PathStatus.Success then local Waypoints = path:GetWaypoints(); for _, Waypoint in pairs(Waypoints) do if Waypoint.Action == Enum.PathWaypointAction.Jump then Humanoid.Jump = true; end; Humanoid:MoveTo(Waypoint.Position); Humanoid.MoveToFinished:Wait(); end; else Humanoid:MoveTo(RootPart.Position); end; end); --Initilization-- RootPart:SetNetworkOwner(nil);