NPC follow script

Hi,

This is my first post and just started programming with Roblox, I have coded before but many years ago.

I am currently making my first game and having issues with the multi-player aspect of Roblox design, I have an NCP script which spawns R6 humanoids and for the first player that joins my game it works great and the NPC follows and tries to kill the player.

The problem is that any new players that join the game the NPC still only follows the first player.

I am using the find closest Humanoid and the NPC is in ReplicatedStorage but have also tried in the WorkSpace and many other things but can’t understand what is wrong.

local hum = script.Parent:FindFirstChild(“Humanoid”)
local torso = script.Parent:FindFirstChild(“HumanoidRootPart”)

Can anyone point me in the correct direction? I can post the code if it helps.

Thanks
CanterCrow

14 Likes

Make a part that detects how much studs the player is away from the player, if it is the studs you want it to be then script the npc’s walkto to where you want it to go.

for example
Dummy.walkto = vector3.new(game.players.localplayer.Torso.position)

2 Likes

Or he can use Magnitude or DistanceFromCharacter to detect if the player is near.

5 Likes

That would help too, Thanks for replying.

So, you need to get your NPC model first. Define it in your script like you have done already.

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

Next you want to make a function which gets the player closest to the NPC.

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

This will return the closest player within a range of whatever you choose. Now time to move the NPC.


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

Hope this helped you with your issue.

27 Likes

Hi,

Thanks for the quick reply and all the advice I think i am doing what you all have quoted my script is below that only works for the first player.

“```”
wait()

local hum = script.Parent:FindFirstChild(“Humanoid”)
local torso = script.Parent:FindFirstChild(“HumanoidRootPart”)

local max_dist = 200

function findNearestTorso()
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character ~= nil then
local target_torso = player.Character:FindFirstChild(“HumanoidRootPart”)
if target_torso ~= nil then
if (target_torso.Position - torso.Position).magnitude < max_dist then
return target_torso
end
end
end
end
end

local old_pos = torso.Position

wait(math.random(0, 90)/100)

while hum ~= nil do
local target = findNearestTorso()
if target ~= nil then
while target ~= nil and target.Parent ~= nil and hum ~= nil do
if (target.Position - torso.Position).magnitude < max_dist then
hum:MoveTo(target.Position, target)
if target.Position.Y > torso.Position.Y + 3 then
hum.Jump = true
end
if (old_pos - torso.Position).magnitude < 0.5 then
if math.random(1, 3) == 1 then
torso.Velocity = torso.CFrame.lookVector * -40
hum:MoveTo(torso.Position + Vector3.new(math.random(-1, 1), 0, math.random(-1, 1))*20)
wait(1.2)
else
hum.Jump = true
hum:MoveTo(target.Position, target)
end
end
else break end
old_pos = torso.Position
wait(0.5)
end
end
wait(4)
end
“```”

Sorry I am formatting correctly and will post in a bit

indent preformatted text by 4 spaces[script.txt|attachment](upload://9VK7wHeN1xJYGz6bEok9JF829hj.txt) (1.3 KB) 

I have attached the code as cant see how to keep the formatting

1 Like

Have you tried using the code I provided?

4 Likes

Hi,

I have tried the code provided and its working, thanks for you help,

J

2 Likes

Target is the closest player, use Target.Character.PrimaryPart.Position (HumanoidRootPart’s Position)

5 Likes