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
for i,v in pairs(game.Players:GetPlayers())do if v.Character then local charHRP = v.Character.HumanoidRootPart if (charHRP.Position-AIHumanoidRootPart.Position).Magnitude < closestPlayerDistance then closestPlayer = v.Character closestPlayerDistance = (charHRP.Position-AIHumanoidRootPart.Position).Magnitude end end end return closestPlayer
end
while true do task.wait()
local ClosestPlayer = GetClosestPlayer() if ClosestPlayer then AIHumanoid:MoveTo(ClosestPlayer.HumanoidRootPart.Position) 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.
local Players = game:GetService("Players") local ChaseRange = 1420 local AIHumanoid = script.Parent:FindFirstChildOfClassWhichIsA("Humanoid") local function GetClosestPlayer() local AICFrame = script.Parent:GetPivot() local closestPlayer = nil local closestPlayerDistance = ChaseRange for i, v in pairs(Players:GetPlayers()) do if v.Character then local charH = v.Character:FindFirstChildWhichIsA("Humanoid") local charCFrame = v.Character:GetPivot() if (charCFrame.Position - AICFrame.Position).Magnitude <= closestPlayerDistance then if charH.Health > 0 then -- if alive closestPlayer = v.Character closestPlayerDistance = (charCFrame.Position - AICFrame.Position).Magnitude end end end end return closestPlayer end while task.wait() do local ClosestPlayer = GetClosestPlayer() if ClosestPlayer then AIHumanoid:MoveTo(ClosestPlayer:GetPivot().Position) end end
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:
if playerHumanoid.Health <= 0 then -- stop following end
If you provide the script, I could also implement a way to stop it incase you dont know how to do that either.