RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"NPC that stalks and kills, avanced and complete script"
-- creating an NPC that stalks and kills players local pfs = game:GetService("PathfindingService") local npc = game.Workspace.NPC local aggroRange = 50 local attackRange = 5 local attackCooldown = 2 local attacking = false local function findNearestPlayer() local nearestPlayer = nil local nearestDistance = math.huge for _, player in ipairs(game.Players:GetPlayers()) do if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then local distance = (npc.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude if distance < nearestDistance then nearestDistance = distance nearestPlayer = player end end end return nearestPlayer, nearestDistance end local function moveToTarget(target) local path = pfs:CreatePath() path:ComputeAsync(npc.HumanoidRootPart.Position, target.Position) for _, waypoint in pairs(path:GetWaypoints()) do npc.Humanoid:MoveTo(waypoint.Position) npc.Humanoid.MoveToFinished:Wait() end end local function attack(target) if not attacking then attacking = true npc.Humanoid:MoveTo(target.HumanoidRootPart.Position) wait(attackCooldown) if (npc.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= attackRange then target.Humanoid:TakeDamage(npc.Humanoid.Health / 2) end attacking = false end end while true do local nearestPlayer, nearestDistance = findNearestPlayer() if nearestPlayer and nearestDistance <= aggroRange then if nearestDistance <= attackRange then attack(nearestPlayer.Character) else moveToTarget(nearestPlayer.Character.HumanoidRootPart) end else wait(1) end end