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 1 year ago
Edited 1 year 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

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

2 answers

Log in to vote
0
Answered by 1 year 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.

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
Ad
Log in to vote
2
Answered by 1 year 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:

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.

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 — 1y
0
I can't read that. Could you put it in the description by using edit? LikeableEmmec 470 — 1y
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 — 1y
0
Oh! T3 already answered. Hope we both help! LikeableEmmec 470 — 1y

Answer this question