I am scripting an AI that uses smart pathfinding until it gets so close but when it finally kills the plyer this error occurs,
I understand why it’s saying it but all my solutions don’t seem to work
local pathFindingService = game:GetService("PathfindingService")
local goal = nil
local humRootPart = script.Parent.HumanoidRootPart
local hum = script.Parent.Humanoid
local function findTarget()
local closestPlayer
local closestChar
local closestDistance = math.huge
for i, v in pairs(game.Players:GetPlayers()) do
local char = v.Character or v.CharacterAdded:Wait(.5)
local hum = char.Humanoid
if char and hum.Health > 0 then
local dist = (humRootPart.Position - char.HumanoidRootPart.Position).Magnitude
if (dist <= closestDistance) then
closestPlayer = v
closestDistance = dist
closestChar = v.Character or v.CharacterAdded:Wait()
end
end
end
return closestChar
end
local function canSeePlayer()
local target = findTarget()
local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
params.FilterType = Enum.RaycastFilterType.Blacklist
local hit, pos = workspace:Raycast(humRootPart.Position, target.HumanoidRootPart.Position - humRootPart.Position, params)
if hit ~= nil and hit.Instance.Parent == target.HumanoidRootPart.Parent then
return true
end
end
local function moveToClosestPlayer()
local seePlayer = canSeePlayer()
local target = findTarget()
local path = pathFindingService:CreatePath()
path:ComputeAsync(humRootPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if seePlayer == true then
for i, v in pairs(waypoints) do
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > 15 and target.Humanoid.Health > 0 then
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > .5 and target.Humanoid.Health > 0 then
local attackAnim = script.Parent.Hatchet.AttackAnimation
local attackAnimPlay = hum:LoadAnimation(attackAnim)
attackAnimPlay:Play()
script.Parent.Hatchet.Handle.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent then
local humHit = hit.Parent:FindFirstChild("Humanoid")
humHit.Health -= 25
wait(3)
end
end)
wait(1)
end
if target.Humanoid.Health > 0 then
hum:MoveTo(target.HumanoidRootPart.Position)
end
end
end
end
end
wait(5)
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner()
end
end
while true do
moveToClosestPlayer()
end
local pathFindingService = game:GetService("PathfindingService")
local goal = nil
local humRootPart = script.Parent.HumanoidRootPart
local hum = script.Parent.Humanoid
local function findTarget()
local closestPlayer
local closestChar
local closestDistance = math.huge
for i, v in pairs(game.Players:GetPlayers()) do
local char = v.Character or v.CharacterAdded:Wait(.5)
local human = char.Humanoid
if char and human.Health > 0 then
local dist = (humRootPart.Position - char.HumanoidRootPart.Position).Magnitude
if (dist <= closestDistance) then
closestPlayer = v
closestDistance = dist
closestChar = v.Character or v.CharacterAdded:Wait()
end
end
end
return closestChar
end
local function canSeePlayer(target)
local origin = humRootPart.Position
local direction = (target.HumanoidRootPart.Position - humRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, hum.Parent)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function moveToClosestPlayer()
local seePlayer = canSeePlayer()
local target = findTarget()
local path = pathFindingService:CreatePath()
path:ComputeAsync(humRootPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if seePlayer == true then
for i, v in pairs(waypoints) do
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > 15 and target.Humanoid.Health > 0 then
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > .5 and target.Humanoid.Health > 0 then
local attackAnim = script.Parent.Hatchet.AttackAnimation
local attackAnimPlay = hum:LoadAnimation(attackAnim)
attackAnimPlay:Play()
script.Parent.Hatchet.Handle.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent then
local humHit = hit.Parent:FindFirstChild("Humanoid")
humHit.Health -= 25
wait(3)
end
end)
wait(1)
end
if target.Humanoid.Health > 0 then
hum:MoveTo(target.HumanoidRootPart.Position)
end
end
end
end
end
wait(5)
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner()
end
end
while true do
moveToClosestPlayer()
end
local pathFindingService = game:GetService("PathfindingService")
local goal = nil
local humRootPart = script.Parent.HumanoidRootPart
local hum = script.Parent.Humanoid
local function findTarget()
local closestPlayer
local closestChar
local closestDistance = math.huge
for i, v in pairs(game.Players:GetPlayers()) do
local char = v.Character or v.CharacterAdded:Wait(.5)
local human = char.Humanoid
if char and human.Health > 0 then
local dist = (humRootPart.Position - char.HumanoidRootPart.Position).Magnitude
if (dist <= closestDistance) then
closestPlayer = v
closestDistance = dist
closestChar = v.Character or v.CharacterAdded:Wait()
end
end
end
return closestChar
end
local function canSeePlayer(target)
local origin = humRootPart.Position
local direction = (target.HumanoidRootPart.Position - humRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, hum.Parent)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function moveToClosestPlayer()
local seePlayer = canSeePlayer()
local target = findTarget()
local path = pathFindingService:CreatePath()
path:ComputeAsync(humRootPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if seePlayer == true then
for i, v in pairs(waypoints) do
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > 15 and target.Humanoid.Health > 0 then
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > .5 and target.Humanoid.Health > 0 then
local attackAnim = script.Parent.Hatchet.AttackAnimation
local attackAnimPlay = hum:LoadAnimation(attackAnim)
attackAnimPlay:Play()
script.Parent.Hatchet.Handle.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent then
local humHit = hit.Parent:FindFirstChild("Humanoid")
humHit.Health -= 25
wait(3)
end
end)
wait(1)
end
if target.Humanoid.Health > 0 then
hum:MoveTo(target.HumanoidRootPart.Position)
end
end
end
end
end
wait(5)
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner()
end
end
while true do
moveToClosestPlayer()
end```
local pathFindingService = game:GetService("PathfindingService")
local goal = nil
local humRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local hum = script.Parent:WaitForChild("Humanoid")
local function findTarget()
local closestPlayer
local closestChar
local closestDistance = math.huge
for i, v in pairs(game.Players:GetPlayers()) do
local char = v.Character -- 0.5? wat
if not char or not char:FindFirstChild("Humanoid") or not char:FindFirstChild("HumanoidRootPart") then
continue
end
local hum = char.Humanoid
if char and hum.Health > 0 then
local dist = (humRootPart.Position - char.HumanoidRootPart.Position).Magnitude
if (dist <= closestDistance) then
closestPlayer = v
closestDistance = dist
closestChar = v.Character or v.CharacterAdded:Wait()
end
end
end
return closestChar
end
local function canSeePlayer()
local target = findTarget()
local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
params.FilterType = Enum.RaycastFilterType.Blacklist
local hit, pos = workspace:Raycast(humRootPart.Position, target.HumanoidRootPart.Position - humRootPart.Position, params)
if hit ~= nil and hit.Instance.Parent == target.HumanoidRootPart.Parent then
return true
end
end
local function moveToClosestPlayer()
local seePlayer = canSeePlayer()
local target = findTarget()
local path = pathFindingService:CreatePath()
path:ComputeAsync(humRootPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if seePlayer == true then
for i, v in pairs(waypoints) do
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > 15 and target.Humanoid.Health > 0 then
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > .5 and target.Humanoid.Health > 0 then
local attackAnim = script.Parent.Hatchet.AttackAnimation
local attackAnimPlay = hum:LoadAnimation(attackAnim)
attackAnimPlay:Play()
script.Parent.Hatchet.Handle.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent then
local humHit = hit.Parent:FindFirstChild("Humanoid")
humHit.Health -= 25
wait(3)
end
end)
wait(1)
end
if target.Humanoid.Health > 0 then
hum:MoveTo(target.HumanoidRootPart.Position)
end
end
end
end
end
wait(5)
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner()
end
end
while true do
moveToClosestPlayer()
end
It’s still the same error under the canSeePlayerFunction()
local function canSeePlayer()
local target = findTarget()
local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
params.FilterType = Enum.RaycastFilterType.Blacklist
local hit, pos = workspace:Raycast(humRootPart.Position, target.HumanoidRootPart.Position - humRootPart.Position, params)
if hit ~= nil and hit.Instance.Parent == target.HumanoidRootPart.Parent then
return true
end
end
if you need to see more context i’m happy to show you cause I’ve tried seeing if it exists and still manage to get the same errors and have checks even if the player is alive and the character exists
local pathFindingService = game:GetService("PathfindingService")
local goal = nil
local humRootPart = script.Parent.HumanoidRootPart
local hum = script.Parent.Humanoid
local function canSeePlayer(target)
local origin = humRootPart.Position
local direction = (target.HumanoidRootPart.Position - humRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, hum.Parent)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeePlayer(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function moveToClosestPlayer()
local seePlayer = canSeePlayer()
local target = findTarget()
local path = pathFindingService:CreatePath()
path:ComputeAsync(humRootPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if seePlayer == true then
for i, v in pairs(waypoints) do
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > 15 and target.Humanoid.Health > 0 then
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > .5 and target.Humanoid.Health > 0 then
local attackAnim = script.Parent.Hatchet.AttackAnimation
local attackAnimPlay = hum:LoadAnimation(attackAnim)
attackAnimPlay:Play()
script.Parent.Hatchet.Handle.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent then
local humHit = hit.Parent:FindFirstChild("Humanoid")
humHit.Health -= 25
task.wait(3)
end
end)
task.wait(1)
end
if target.Humanoid.Health > 0 then
hum:MoveTo(target.HumanoidRootPart.Position)
end
end
end
end
end
task.wait(5)
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner()
end
end
while true do
moveToClosestPlayer()
end
nvm check if the target is not nil which you not did, So it means you are doing nil.HumanoidRootPart.Position
local pathFindingService = game:GetService("PathfindingService")
local goal = nil
local humRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local hum = script.Parent:WaitForChild("Humanoid")
local function findTarget()
local closestPlayer
local closestChar
local closestDistance = math.huge
for i, v in pairs(game.Players:GetPlayers()) do
local char = v.Character -- 0.5? wat
if not char or not char:FindFirstChild("Humanoid") or not char:FindFirstChild("HumanoidRootPart") then
continue
end
local hum = char.Humanoid
if char and hum.Health > 0 then
local dist = (humRootPart.Position - char.HumanoidRootPart.Position).Magnitude
if (dist <= closestDistance) then
closestPlayer = v
closestDistance = dist
closestChar = v.Character or v.CharacterAdded:Wait()
end
end
end
return closestChar
end
local function canSeePlayer(target)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
params.FilterType = Enum.RaycastFilterType.Blacklist
local hit, pos = workspace:Raycast(humRootPart.Position, CFrame.lookAt(humRootPart.Position, target.HumanoidRootPart.Position).LookVector * 300, params)
if hit ~= nil and hit.Instance.Parent == target.HumanoidRootPart.Parent then
return true
end
end
local function moveToClosestPlayer()
local target = findTarget()
if not target then
return
end
local seePlayer = canSeePlayer(target)
local path = pathFindingService:CreatePath()
path:ComputeAsync(humRootPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if seePlayer == true then
for i, v in pairs(waypoints) do
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > 15 and target.Humanoid.Health > 0 then
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
if (humRootPart.Position - target.HumanoidRootPart.Position).Magnitude > .5 and target.Humanoid.Health > 0 then
local attackAnim = script.Parent.Hatchet.AttackAnimation
local attackAnimPlay = hum:LoadAnimation(attackAnim)
attackAnimPlay:Play()
script.Parent.Hatchet.Handle.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent then
local humHit = hit.Parent:FindFirstChild("Humanoid")
humHit.Health -= 25
wait(3)
end
end)
wait(1)
end
if target.Humanoid.Health > 0 then
hum:MoveTo(target.HumanoidRootPart.Position)
end
end
end
end
end
wait(5)
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
v:SetNetworkOwner()
end
end
while true do
moveToClosestPlayer()
end