Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Pathfinind NPC with a detection radius help?

Please help me, I want to create a pathfinding npc that attacks and I want to make it so whenever a player is touching a block it will follow him but if he isn't it won't

The basic script is

local sp = script.Parent
local newCharacter = sp:Clone()
newCharacter.Parent = game.ServerStorage
wait(0.5)
pathFind = game:GetService("PathfindingService")
local humanoid = sp:WaitForChild("Humanoid")
local torso = sp:WaitForChild("Torso")
local target = sp:WaitForChild("Target")
local location = sp:WaitForChild("Location")

local sp = script.Parent
local humanoid = sp:WaitForChild("Humanoid")
local toolNone = script:FindFirstChild("toolnone")
local toolNoneAnim = toolNone:FindFirstChild("ToolNoneAnim")

local a = Vector3.new(100,5,-100)
local b = Vector3.new(75,5,-200)
local debounce = true
local debounce2 = false
local toolNoning = humanoid:LoadAnimation(toolNoneAnim)
local list = game.Players:GetChildren()
local loco = 100000

sp.PrimaryPart = sp:WaitForChild("Head")


if target.Value then
	local targetTorso = target.Value:FindFirstChild("Torso")
end

function attack(hit)
	if hit and hit.Parent then
		local enemy = hit.Parent:FindFirstChild("Humanoid")
		if enemy then
			if enemy:IsA("Humanoid") and hit.Parent.Name ~= sp.Name then
				debounce = true
				while debounce do
					toolNoning:Play()
					wait(1)
					torso.TouchEnded:connect(function(hit2)
						if hit2 == hit then
							debounce = false
						end
					end)
				end
			end
		else
			if hit.CanCollide then
				humanoid.Jump = true
			end
		end
	end
end

function findPlayers()
	list = game.Players:GetChildren()
	for i=1,#list do
		if list[i] and list[i]:IsA("Player") then
			local found = list[i].Character:FindFirstChild("Humanoid")
			if found and found.Health>0 then
				local loc = (torso.Position - list[i].Character.Torso.Position).magnitude
				if loc < loco then
					loco = loc
					target.Value = list[i].Character
				end
			end
		end
	end
end


while humanoid.Health > 0 do
	target = sp:WaitForChild("Target")
	location = sp:WaitForChild("Location")
	findPlayers()
	if target.Value then
		targetTorso = target.Value:FindFirstChild("Torso")
		if targetTorso then
			path = pathFind:ComputeRawPathAsync(torso.Position,targetTorso.Position,500)
			points = path:GetPointCoordinates()
			if #points <10 or #points == 10 then
				mx = #points
				l = true
			else
				mx = 10
				l = false
			end
			for i = 1,mx/2 do
				wait(0.25)
				humanoid:MoveTo(points[i*2])
				if i == mx/2 and l then
					humanoid:MoveTo(targetTorso.Position,targetTorso)
				end
			end
		end
	else
		path = pathFind:ComputeRawPathAsync(torso.Position,location.Value,500)
		points = path:GetPointCoordinates()
		if #points <10 then
			mx = #points
		else
			mx = 10
		end
		for i = 1,mx do
			humanoid:MoveTo(points[i])
			wait(0.25)
		end
		humanoid:MoveTo(location.Value)
	end
	loco = 100000
	torso.Touched:connect(attack)
	wait(0.5)
end

if humanoid.Health == 0 or humanoid.Health < 0 then
	wait(5)
	newCharacter.Parent = Workspace
	wait(0.1)
	sp:Destroy()
end

1 answer

We don't have enough time, nor stamina, to rake through your code to discover what isn't working or to make implementation suggestions.

The phrase that you might hear, if you stick around Scripting Helpers, is "this isn't a request site" - we're not here to do your work for you, but to help you understand and point in you in the right direction.

When I read your question, I immediately have a lot of unanswered questions which prevent me from helping you:

How much do you know about scripting? Can you write anything in Lua? Is this your own code? What have you tried so far? Have you tried anything? What is the problem? What should be happening that isn't happening? Etc...

You can't expect us to deduce what your script is doing, identify any problems, and/or add functionality.

Start here, please: https://scriptinghelpers.org/about

Then here: http://wiki.roblox.com/index.php?title=Intro_to_Scripting

This discussion is preserved as it was. New answers, comments, and votes are closed.