How do I run a pcall function

I was working on an npc script. I am trying to make the npc follow the player but when I wrapped the function in a pcall, but I don’t know how to call it when it’s needed.

script:

local PathfindingService = game:GetService("PathfindingService")

local AttackNPC = script.Parent
local zombie = AttackNPC:FindFirstChild("zombie")
local NPCRootPart = AttackNPC:FindFirstChild("NPCRootPart")
local isNPCBlocking = AttackNPC:FindFirstChild("isNPCBlocking")

charactersFound = false 
nearestCharacterFound = false
target = nil

whoHitNPC = {}

local function checkDistance(part1,part2)
	magnitude = (part1.Position - part2.Position).Magnitude
	return magnitude
end

	scanningForTarget = pcall(function()
		for i,v in pairs(game:GetChildren("Model")) do
			if v:IsA("Model") then
				if v ~= AttackNPC then
					if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
						local playerCharacter = v
						characterRootPart = v:FindFirstChild("HumanoidRootPart")
						print("Characters found!")
						charactersFound = true
						moveToTarget()
					end
				end
			end
		end
	end)
end


function moveToTarget()
	if checkDistance(NPCRootPart,characterRootPart) < 20 then
		target = characterRootPart
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(NPCRootPart.Position - characterRootPart.Position)
		local waypoints = path:GetWaypoints()
		for _,waypoint in pairs(waypoints) do
			zombie:MoveTo(target.Position)
			zombie.MoveToFinished:Wait()
			if target.Parent.Humanoid.Health <= 0 then
				target = nil
				scanningForTarget
			end
		end
	end
end

while wait() do
	scanningForTarget
end

Hello, I have a question. I don’t understand your script 100%, so I would like to know why you are using a pcall?

pcall(function()
 --code you want to run
end)

There was an error saying “Lacking permission 6” so I had to wrap it in a pcall

Pcalls should only be used when you’re handling something like web requests or data stores

-- example
local success, result = pcall(function()
   return DataStore:GetAsync("key")
end)

if success then
   return result
else
   warn("unable to load data")
end
1 Like

if its lacking permission the code will neve run no matter what. pcall just silence errors, almost like try {} in c++

It’s because game:GetChildren get’s children that aren’t normally accessible through normal means like browsing

How do I avoid the “Lacking permission 6” then?

You can’t avoid it, you should use workspace:GetChildren() if you’re trying to find a certain character object

1 Like

instead of explicitly getting all the models use a implicit method like

for _, model in pairs(workspace:GetDescendants()) do
  if v:IsA("Model") then
    --code
  end
end

Wait a second, I just realized something… while looking over your reply and my code I missed something, I did "game:GetChildren(“Models”) instead of game.Workspace:GetChildren(“Models”) I dont know if that makes a difference

I thought you couldnt pass a parameter to get children so i assumed it was like some deprecated stuff is why

1 Like

You can’t

https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren

Yea I realized it. The script works now. Here it is

local AttackNPC = script.Parent
local zombie = AttackNPC:FindFirstChild("zombie")
local NPCRootPart = AttackNPC:FindFirstChild("NPCRootPart")
local isNPCBlocking = AttackNPC:FindFirstChild("isNPCBlocking")

charactersFound = false 
nearestCharacterFound = false
target = nil

whoHitNPC = {}

local function checkDistance(part1,part2)
	magnitude = (part1.Position - part2.Position).Magnitude
	return magnitude
end

function scanningForTarget()
	for i,v in pairs(game.Workspace:GetDescendants("Model")) do
		if v:IsA("Model") then
			if v ~= AttackNPC then
				if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
					local playerCharacter = v
					characterRootPart = v:FindFirstChild("HumanoidRootPart")
					print("Characters found!")
					charactersFound = true
					moveToTarget()
				end
			end
		end
	end
end

function moveToTarget()
	if checkDistance(NPCRootPart,characterRootPart) < 20 then
		target = characterRootPart
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(NPCRootPart.Position,characterRootPart.Position)
		local waypoints = path:GetWaypoints()
		for _,waypoint in pairs(waypoints) do
			zombie:MoveTo(target.Position)
			zombie.MoveToFinished:Wait()
			if target.Parent.Humanoid.Health <= 0 then
				target = nil
				scanningForTarget()
			end
		end
	end
end

function main()
	if target ~= nil then
		return true
	elseif target == nil then
		scanningForTarget()
	end
end

while wait() do
	main()
end

Just made a couple of changes now it works!

2 Likes

I think you meant to say workspace:GetChildren() here.