How do i make ai that follows you but stops trying to follow the player when the player is dead since i made breakjointsondeath to false ,the ai just flings the player
i see alot of tutorials on yt but none of them make the ai stop following after the player is dead
Here is the script I dont remember who made this script but i remember taking this from a youtube video:
local ChaseRange = 1420
local AIHumanoid = script.Parent.Humanoid local AIHumanoidRootPart = script.Parent.HumanoidRootPart
local function GetClosestPlayer() local closestPlayer = nil local closestPlayerDistance = ChaseRange
01 | for i,v in pairs (game.Players:GetPlayers()) do |
02 | if v.Character then |
03 | local charHRP = v.Character.HumanoidRootPart |
04 |
05 | if (charHRP.Position-AIHumanoidRootPart.Position).Magnitude < closestPlayerDistance then |
06 | closestPlayer = v.Character |
07 | closestPlayerDistance = (charHRP.Position-AIHumanoidRootPart.Position).Magnitude |
08 | end |
09 | end |
10 | end |
11 |
12 | return closestPlayer |
end
while true do task.wait()
1 | local ClosestPlayer = GetClosestPlayer() |
2 |
3 | if ClosestPlayer then |
4 | AIHumanoid:MoveTo(ClosestPlayer.HumanoidRootPart.Position) |
5 | end |
end
@LikeableEmmec is correct, you should check if player is alive or dead by checking the player's health is above, equals to, or below zero.
Here is the script, you can copy-and-paste this directly to your code.
01 | local Players = game:GetService( "Players" ) |
02 |
03 | local ChaseRange = 1420 |
04 |
05 | local AIHumanoid = script.Parent:FindFirstChildOfClassWhichIsA( "Humanoid" ) |
06 |
07 | local function GetClosestPlayer() |
08 | local AICFrame = script.Parent:GetPivot() |
09 |
10 | local closestPlayer = nil |
11 | local closestPlayerDistance = ChaseRange |
12 |
13 | for i, v in pairs (Players:GetPlayers()) do |
14 | if v.Character then |
15 | local charH = v.Character:FindFirstChildWhichIsA( "Humanoid" ) |
Well, you can use an if
statement to check if the player's health is below or equal to zero. Relatively simple code, wont stretch this out:
1 | if playerHumanoid.Health < = 0 then |
2 | -- stop following |
3 | end |
If you provide the script, I could also implement a way to stop it incase you dont know how to do that either.