Getting an NPC to move towards a character in a radius of 10 studs

I tried, i dont get any errors but the npc is not moving towards me.

also yes his name is tort dont ask

local torthumanoidroot = script.Parent.Torso
local radius = 10
local nearest = nil
local dist = math.huge
-- get all torsos in a radius of 10 and find the nearest one
local function getNearestTorso()
    --local nearest = nil
    --local dist = math.huge
	for _, torso in pairs(game.Workspace:GetDescendants()) do
		wait(0.02)
        if torso:IsA("BasePart") and torso.Name == "Torso" and torso.Parent ~= game.Workspace.TORT and torso.Parent ~= game.Workspace[" "] then
            local magnitude = (torso.Position - torthumanoidroot.Position).magnitude
            if magnitude < radius and magnitude < dist then
                nearest = torso.Parent.HumanoidRootPart
                dist = magnitude 
            end 
        end 
    end 
    return nearest, dist ~= math.huge -- returns the nearest part and a boolean indicating whether or not there is a nearby part.
end 
while true do
	getNearestTorso()
	if dist <= radius then
	wait(0.1)
		script.Parent.State.Value = 1 -- states are what i am using to track the actions of an npc, state 0 is wandering and state 1 is chasing
	script.Parent.Zombie.WalkToPoint = nearest.Position
	print(nearest .. dist)
	else
		state = 0
	end
end
3 Likes

very bad method you are looping through every instance in the game instead you can loop through game.Players:GetChildren() and then get the all the players torsos from that and do the comparrison

for I, Player in pairs(game.Players:GetChildren()) do
	local CurrentChar = Player.Character
	local CurrentTorso = CurrentChar.Torso
    --compare distance
end
1 Like

That doesn’t seem to fix anything but it does optimzie the script, heres the new script.

local radius = 10
local nearest = nil
local hum = script.Parent.Zombie
local state = script.Parent.State
while true do
	wait(0.1)
	if state.Value == 0 then
		local newpos = hum.WalkToPoint + Vector3.new(math.random(-70, 70),0,math.random(-70, 70))
		hum:MoveTo(newpos)
		wait(math.random(0.1, 4))
	end
end

local dist = math.huge
-- get all torsos in a radius of 10 and find the nearest one
local function getNearestTorso()
for I, Player in pairs(game.Players:GetChildren()) do
		local CurrentChar = Player.Character
		local CurrentTorso = CurrentChar.Torso
            local magnitude = (CurrentTorso.Position - torttorso.Position).magnitude
            if magnitude < radius and magnitude < dist then
                nearest = CurrentTorso
			dist = magnitude
			return nearest, dist ~= math.huge
			end
			print(nearest .. dist)-- returns the nearest part and a boolean indicating whether or not there is a nearby part.
		end
	end

while true do
	wait(0.02)
getNearestTorso()
	if dist <= radius then
		script.Parent.State.Value = 1
	script.Parent.Zombie.WalkToPoint = nearest.Position
	print(nearest .. dist)
	else
		script.Parent.State.Value = 0
	end
end```
1 Like

more scrutinys i have you have the wait for your while loop in a conditional statement dangerous if the condition does run true then it will run with a wait
and you arent returning ur function to any variables some other stuff like placement of conditional statements but whatever heres my fix obviously i cant test to see if works but u can:


local torthumanoidroot = script.Parent.Torso
local radius = 10
local nearest = nil
local dist = math.huge
-- get all torsos in a radius of 10 and find the nearest one
local function getNearestTorso()
	local nearest = nil
	local dist = 10000000000
	for _, torso in pairs(game.Workspace:GetDescendants()) do
		wait(0.02)
		if torso:IsA("BasePart") and torso.Name == "Torso" and torso.Parent ~= game.Workspace.TORT and torso.Parent ~= game.Workspace[" "] then
			local magnitude = (torso.Position - torthumanoidroot.Position).magnitude
			if magnitude < radius and magnitude < dist then
				nearest = torso.Parent.HumanoidRootPart
				dist = magnitude 
			end 
		end 
	end 
	if dist < radius then
		return nearest
	end
end 
while true do
	local Nearest = getNearestTorso()
	if Nearest ~= nil then
		script.Parent.State.Value = 1 -- states are what i am using to track the actions of an npc, state 0 is wandering and state 1 is chasing
		script.Parent.Zombie.WalkToPoint = nearest.Position
		print(nearest .. dist)
	end
	task.wait(0.1)
end
1 Like
local Player = game:GetService("Players")

local Character = script.Parent
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local StateIndicator = Character:WaitForChild("State")

local CurrentlyWandering = false
local RNG = Random.new()

local ChaseDistance = 10

local function FindClosestPlayer()
	local ClosestPlayer, ClosestDistance, PlayerPosition = nil, math.huge, nil

	for _, Player in Player:GetChildren() do
		local PlayerCharacter = Player.Character

		if not PlayerCharacter then
			continue
		end

		local CharacterHumanoid = PlayerCharacter:FindFirstChildWhichIsA("Humanoid")

		if not CharacterHumanoid or CharacterHumanoid.Health == 0 then
			continue
		end

		local PlayerCharacterPosition = PlayerCharacter:GetPivot().Position
		local Distance = (Character:GetPivot().Position - PlayerCharacterPosition).Magnitude

		if Distance < ClosestDistance then
			ClosestPlayer = Player
			ClosestDistance = Distance
			PlayerPosition = PlayerCharacterPosition
		end
	end
	return ClosestPlayer, PlayerPosition
end

Humanoid.MoveToFinished:Connect(function()
	CurrentlyWandering = false
end)

while true do
	local ClosestPlayer, PlayerPosition = FindClosestPlayer()

	if ClosestPlayer then
		-- chase
		CurrentlyWandering = false
		Humanoid:MoveTo(PlayerPosition)
		StateIndicator.Value = 1
	elseif not CurrentlyWandering then
		-- wander
		CurrentlyWandering = true
		Humanoid:MoveTo(Character:GetPivot().Position + Vector3.new(RNG:NextInteger(-70, 70), 0, RNG:NextInteger(-70, 70)))
		StateIndicator.Value = 0
	end

	task.wait(.1)
end
1 Like

doesnt work but helps me to understand how to optimize stuffs

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.