Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Ai following dead bodys?

Asked by 2 years ago
Edited 2 years ago

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

01for 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
10end
11 
12return closestPlayer

end

while true do task.wait()

1local ClosestPlayer = GetClosestPlayer()
2 
3if ClosestPlayer then
4    AIHumanoid:MoveTo(ClosestPlayer.HumanoidRootPart.Position)
5end

end

2 answers

Log in to vote
0
Answered by 2 years ago

@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.

01local Players = game:GetService("Players")
02 
03local ChaseRange = 1420
04 
05local AIHumanoid = script.Parent:FindFirstChildOfClassWhichIsA("Humanoid")
06 
07local 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")
View all 36 lines...
Ad
Log in to vote
2
Answered by 2 years ago

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:

1if playerHumanoid.Health <= 0 then
2    -- stop following
3end

If you provide the script, I could also implement a way to stop it incase you dont know how to do that either.

0
Here is the script 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-AIHum ImNotAN00b_0kay 31 — 2y
0
I can't read that. Could you put it in the description by using edit? LikeableEmmec 470 — 2y
0
I edited it The first part didnt count as script so make sure to copy that also BTW I DONT REMEMBER WHO MADE THE SCRIPT ImNotAN00b_0kay 31 — 2y
0
Oh! T3 already answered. Hope we both help! LikeableEmmec 470 — 2y

Answer this question