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