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