Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

how would I fix these errors in a NPC follow script?

Asked by 4 months ago
Edited 3 months ago

honestly Idk how many errors there are i got this online so please do a check for viruses as well thx! :)

local NPC = script.Parent
local Humanoid = script.Parent:FindFirstChild("Humanoid")
local HRP = script.Parent:FindFirstChild("HumanoidRootPart")
local function GetClosestPlayer(range)
    local List = {}
    local Target = nil
    for i, v in pairs(game.Players:GetPlayers()) do
        local dist = v:DistanceFromCharacter(HRP.position)
        if dist <= range then
            table.insert(List, {dist, v})
        end
    end

    table.sort(List, function(A, B)
        return A[1] < B[1]
    end)

    pcall(function()
        Target = List[1][2]
    end)

    return Target
end

local NPC = script.Parent
local Humanoid = script.Parent:FindFirstChild("Humanoid")
local HRP = script.Parent:FindFirstChild("HumanoidRootPart")

local function GetClosestPlayer(range)
    local List = {}
    local Target = nil
    for i, v in pairs(game.Players:GetPlayers()) do
        local dist = v:DistanceFromCharacter(HRP.position)
        if dist <= range then
            table.insert(List, {dist, v})
        end
    end

    table.sort(List, function(A, B)
        return A[1] < B[1]
    end)

    pcall(function()
        Target = List[1][2]
    end)

    return Target
end

while true do
    wait()
    local Target = getClosestPlayer(30) -- // change 30 to the max distance
    if Target == nil then return end
    Humanoid:MoveTo(Target.PrimaryPart)
    Humanoid.MoveToFinished:Wait()
end

Ps: I dont have the errors I kinda left the game alone and never went back to it lol its somewhere XD

0
"just fix them" isn't the best way to learn or get help. Exanut 81 — 4mo
0
like Exanut said, don't just come here asking for people to fix your script. Also, if you even want any help on how to fix it atleast provide a picture, or maybe just type here what the errors are. NvidiaGeForce2080Ti -5 — 4mo
0
sorry i know that was a bad idea superspeedr1234 60 — 4mo
0
it would be better to ask for help by showing the code then the errors that your getting in output ErraticThe_legend27 8 — 3mo

Answer this question