hey so when my police man npc kills another npc it says attempt to index nil with findfirstchild, and i want to fix that
so ive tried to look at the arguments that my functions in my module script needs
and it seems that everything is filled out so idk whats happening.
heres my module and server script:
local policemodule = {}
function wander(hum, root, character)
local area = Vector3.new(root.Position.X + math.random(-30, 30), 0, root.Position.Z + math.random(-30, 30))
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath({
AgentCanJump = true,
AgentCanClimb = true
})
path:ComputeAsync(root.Position, area)
for i, waypoint in pairs(path:GetWaypoints()) do
local waypoints = path:GetWaypoints()
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(waypoint.Position)
local timeout = hum.MoveToFinished:Wait(1)
local nearest = findnearest(root)
if raycastsight(nearest, character) then
policemodule.walktotarget(root, hum, character)
break
end
if not timeout then
hum.Jump = true
wander(hum, root, character)
break
end
end
end
function findnearest(root)
local maxdist = 300
local nearest = nil
for i, v in pairs(workspace:GetChildren()) do
local vhum = v:FindFirstChild("Humanoid")
local vroot = v:FindFirstChild("HumanoidRootPart")
local status = v:FindFirstChild("Type")
if vhum and vroot and status.Value == "Criminal" and vhum.Health > 0 then
if nearest == nil then
nearest = vroot
end
if (root.Position - vroot.Position).magnitude <= maxdist then
maxdist = (root.Position - vroot.Position).magnitude
nearest = vroot
end
end
end
return nearest
end
function policemodule.walktotarget(root, hum, character)
local nearesttarget = findnearest(root)
local iseeyou = raycastsight(nearesttarget, character)
if nearesttarget then
if nearesttarget.Parent:FindFirstChild("Type").Value == "Criminal" then
if nearesttarget.Parent:FindFirstChild("Humanoid").Health > 0 then
if iseeyou then
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath({
AgentCanJump = true,
AgentCanClimb = true
})
path:ComputeAsync(root.Position, nearesttarget.Position)
for i, waypoint in pairs(path:GetWaypoints()) do
local waypoints = path:GetWaypoints()
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
if (root.Position - waypoints[1].Position).magnitude > 40 then
policemodule.walktotarget(root, hum, character)
break
end
hum:MoveTo(waypoint.Position)
local timeout = hum.MoveToFinished:Wait(1)
if (root.Position - nearesttarget.Position).magnitude < 14 then
local c = root.CFrame * CFrame.new(0, 0, 14)
hum.AutoRotate = false
hum:MoveTo(c.Position)
break
end
if (root.Position - nearesttarget.Position).magnitude > 7 then
local c = root.CFrame * CFrame.new(0, 0, -7)
hum.AutoRotate = true
hum:MoveTo(c.Position)
end
if not timeout then
hum.Jump = true
policemodule.walktotarget(root, hum, character)
break
end
end
else
local knownnearest = game.ReplicatedStorage:FindFirstChild(nearesttarget.Parent.Name)
if knownnearest then
if knownnearest.Value ~= Vector3.new(0, 0, 0) then
comms(nearesttarget, root, hum, character)
else
wander(hum, root, character)
end
end
end
else
local known = game.ReplicatedStorage:FindFirstChild(nearesttarget.Parent.Name)
if known then
known.Value = Vector3.new(0, 0, 0)
end
end
else
local known = game.ReplicatedStorage:FindFirstChild(nearesttarget.Parent.Name)
if known then
known.Value = Vector3.new(0, 0, 0)
end
end
else
wander(hum, root, character)
end
end
function raycastsight(nearest, character)
if nearest then
local direction = (nearest.Position - character.Head.Position).Unit * 300
local raycastparams = RaycastParams.new(
character.Head,
character.Torso,
character["Right Arm"],
character["Left Arm"],
character["Right Leg"],
character["Left Leg"]
)
raycastparams.FilterType = Enum.RaycastFilterType.Exclude
local raycast = workspace:Raycast(character.Head.Position, direction, raycastparams)
if raycast then
if raycast.Instance.Parent:FindFirstChild("Humanoid") and raycast.Instance.Parent:FindFirstChild("HumanoidRootPart") and raycast.Instance.Parent:FindFirstChild("Type").Value == "Criminal" then
if not game.ReplicatedStorage:FindFirstChild(nearest.Parent.Name) then
local known = Instance.new("Vector3Value")
known.Parent = game.ReplicatedStorage
known.Name = nearest.Parent.Name
known.Value = raycast.Position
else
game.ReplicatedStorage:FindFirstChild(nearest.Parent.Name).Value = raycast.Position
end
shoot(character.HumanoidRootPart, raycast.Position)
return true
else
return false
end
end
end
end
function shoot(root, placetoshoot)
if placetoshoot then
bullet(root.Position, placetoshoot)
task.wait(0.3)
end
end
function bullet(startingpos, direction)
local bullet = Instance.new("Part")
bullet.Parent = workspace
bullet.Anchored = true
bullet.Size = Vector3.new(0.25, 0.25, 5)
bullet.BrickColor = BrickColor.new("New Yeller")
bullet.Material = Enum.Material.Neon
bullet.CastShadow = false
bullet.Name = "Bullet"
bullet.CanCollide = false
bullet.CFrame = CFrame.new(startingpos, direction)
local bulletscript = game.ReplicatedStorage.BulletMover:Clone()
bulletscript.Parent = bullet
bulletscript.Enabled = true
end
function comms(nearest, root, hum, character)
local area = game.ReplicatedStorage:FindFirstChild(nearest.Parent.Name)
if area then
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath({
AgentCanJump = true,
AgentCanClimb = true
})
path:ComputeAsync(root.Position, area.Value)
for i, waypoint in pairs(path:GetWaypoints()) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait(1)
if hum.MoveToFinished then
local sight = raycastsight(nearest, character)
if sight then
policemodule.walktotarget(root, hum, character)
end
end
if nearest then
local waypoints = path:GetWaypoints()
if (root.Position - waypoints[#waypoints].Position).magnitude < 3 then
local sight = raycastsight(nearest, character)
if (waypoints[#waypoints].Position - nearest.Position).magnitude < 20 or sight == false then
area.Value = Vector3.new(0, 0, 0)
policemodule.walktotarget(root, hum, character)
break
else
policemodule.walktotarget(root, hum, character)
end
end
end
end
end
end
return policemodule
local module = game.ReplicatedStorage.ModuleScript
local req = require(module)
while true do
task.wait(1)
req.walktotarget(script.Parent.HumanoidRootPart, script.Parent.Humanoid, script.Parent)
end
if u need anything ask me


