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

How to make an NPC Following script with radius?

Asked by 5 years ago

Its simple, and probably a LOT of people can do it, but iv never tried this before considering iv been scripting for how long now? years. So i have tried this...

(script inside NPC)

--something that detects a player WITHING A RADIUS!!:Connect(function(c)
script.Parent.Humanoid:MoveTo(c.HumanoidRootPart.CFrame  - radius )--i dont want NPC going directly towards player
end)

Any help is appreciated :)

Please Help

0
just to clarify, you want the npc walking towards the player? s_iara 94 — 5y
0
lol, he specifically stated in the question for the npc to walk within a certain radius of the player, but not to the exact location of the player SerpentineKing 3885 — 5y
0
yes, but within a radius of like 10 bricks, i want NPC to stop within that radius, please answer this CommanderCaubunsia 126 — 5y
0
serpentine im sorry, i was CLARIFYING with COMMANDER tyvm s_iara 94 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Omg im sooo sorry but it didnt work, i tried

wait(1)
follow = false
Character = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)

while true do
 if game.Players.LocalPlayer.Character.HumanoidRootPart ~= nil then
  if game.Players.LocalPlayer.Character.HumanoidRootPart.Position.X - game.Workspace.NPC.HumanoidRootPart.Position.X <= 10 then
   follow = true
  end
 end

 if follow == true then
  game.Workspace.NPC.Humanoid.WalkToPoint = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
 end
 wait(.5)
end

The (unanchored) person named NPC does not follow me...

Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago
Edited 5 years ago

You could get the nearest player by using tables and the table.sort() method:

local SearchDistance = 10000

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

local function getNearestPlayer()
    local playerDistances = {}
    if HRP then 
        for i, player in pairs(game.Players:GetPlayers() do 
            -- get the distance between the player's character and the HRP's position
            local distance = player:DistanceFromCharacter(HRP.Position)
            -- 'table.insert(x, y, z)' has 3 arguments. x = table(array), y = number position (optional), z = variable/object/instance your adding to the table
            table.insert(
                playerDistances,
                {
                    player,
                    distance
                {
            )
        end
    end
    if #playerDistances >= 1 then 
        --'table.sort(x, y)' has 2 arguments. x = table(array), y = function (optional)
        table.sort(
            playerDistances,
            function(a, b) -- 'a' and 'b' is the data/table inserted into the 'playerDistances' table
                -- we are comparing the distances and ordering them from least to greatest
                -- so we get the data's second value 'a[2]' and 'b[2]' which is the distances
                return a[2] < b[2]
            end
        )
        for i, dataTable in pairs(playerDistances) do  
            local player = dataTable[1]
            local distance = dataTable[2]
            if player and distance then 
                -- 'i' is the number position of the 'dataTable'
                if i == 1 and distance < SearchDistance then 
                    return player
                end
            end
        end
    end
end

while HRP and Humanoid.Health > 0 do 
    wait(1)
    local nearestPlayer = getNearestPlayer()
    if nearestPlayer then 
        print("player nearby:", nearestPlayer)
    end
end

Now with this you can figure out to stop the NPC from going over the radius. I have to go rn, attempt it and if you need any help leave a comment.

0
ok thx ill try, wait this looks like it should be in normal script right? nvm ill try both. CommanderCaubunsia 126 — 5y
0
so sorry it didnt work though CommanderCaubunsia 126 — 5y
Log in to vote
-1
Answered by
s_iara 94
5 years ago
Edited 5 years ago
wait(1)

--variables 
follow = false
Character = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)

while true do
 if game.Players.LocalPlayer.Character.Humanoid.RootPart ~= nil then
  if game.Players.LocalPlayer.Character.Humanoid.RootPart.Position.X - --finds the character's position
game.Workspace.[NPCNAME].Humanoid.RootPart.Position.X <= 10 then --determines if the npc is within the 10 radius 
   follow = true --if you're in the radius it will follow you
  end
 end

 if follow then
  game.Workspace.[NPCNAME].Humanoid.WalkToPoint = game.Players.LocalPlayer.Character.Humanoid.RootPart.Position --this just tells the npc to walk towards you/ tells what 'following' will do
 end
 wait(.5)
end?

--Change the "NPCNAME" to your NPC's name of course --Good luck

0
You might want to try it yourself, not too sure if it works s_iara 94 — 5y
0
ok CommanderCaubunsia 126 — 5y
0
It's also probably not what you wanted, but hopefully you have an idea s_iara 94 — 5y
0
please explain your answer rather than just giving code, and please make you code FE compatable theking48989987 2147 — 5y
0
no working!, and by the way i did not ask for follow when within radius, i want NPC to stop following me when within that radius!Follow me when im OUT the radius CommanderCaubunsia 126 — 5y

Answer this question