The NPC Moves through/into the Zone we have designated as math.huge… No idea what is wrong with it. I know the code is very messy but yea, if got any suggestions…
Zones
PathfindingModifier
SafezonePart
The Full Code:
local animation1 = script:WaitForChild('Normal')
local animation2 = script:WaitForChild("Jump")
local animation3 = script:WaitForChild("Run")
local humanoid = script.Parent:WaitForChild('Humanoid')
local walk = humanoid:LoadAnimation(animation1)
local run = humanoid:LoadAnimation(animation3)
local jump = humanoid:LoadAnimation(animation2)
local pathfinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local IE = RS.Events.InteractEvent
local PS = game:GetService("Players")
local rootPart = script.Parent:WaitForChild("HumanoidRootPart") or script.Parent:WaitForChild("Torso")
local humanoid = script.Parent:WaitForChild("Humanoid")
local destination = false
local cd = false
local scd = false
local scc = 0
local sca = 30
local countcd = 0
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1500
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart") or temp2:FindFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
function hit(destination,rootPart)
local player = PS:GetPlayerFromCharacter(destination.Parent)
if player then
local targetp = rootPart.CFrame.LookVector * 5000
IE:FireClient(player, "RIP",targetp)
end
destination.Parent.Humanoid.Health = destination.Parent.Humanoid.Health - 50
end
wait()
rootPart:SetNetworkOwner(nil)
while true == true do
wait(1)
local destination = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if destination ~= nil then
local path = pathfinding:CreatePath({
AgentCanJump = false,
Costs = {
Safezone = math.huge
}
})
path:ComputeAsync(rootPart.Position,destination.Position)
local wps = path:GetWaypoints()
for i,v in pairs(wps) do
if v.Action == Enum.PathWaypointAction.Jump then
jump:Play()
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
local jumping = true
while humanoid.Jumping do
wait(0.1)
end
else
if (destination.Position - script.Parent.HumanoidRootPart.Position).magnitude < 10 then
hit(destination,rootPart)
break
elseif (destination.Position - script.Parent.HumanoidRootPart.Position).magnitude < 150 then
if path:CheckOcclusionAsync(i) == -1 and path.Status == Enum.PathStatus.Success then
humanoid:MoveTo(destination.Position)
if scd == false then
script.Parent.Jumpscare2:Play()
scd = true
else
if scc >= sca then
scc = 0
scd = false
else
scc = scc + 1
end
end
run:Play()
humanoid.WalkSpeed = 30
else
end
else
humanoid:MoveTo(v.Position)
if not cd then
cd = true
if (destination.Position - script.Parent.HumanoidRootPart.Position).magnitude < 300 then
if scd == false then
script.Parent.Jumpscare2:Play()
scd = true
else
if scc >= sca then
scc = 0
scd = false
else
scc = scc + 1
end
end
run:Play()
humanoid.WalkSpeed = 30
else
walk:Play()
humanoid.WalkSpeed = 10
end
else
if countcd > ((destination.Position - script.Parent.HumanoidRootPart.Position).magnitude / 20 ) then
cd = false
countcd = 0
break
else
countcd = countcd + 1
end
end
end
humanoid.MoveToFinished:Wait()
end
end
jump:Stop()
run:Stop()
walk:Stop()
end
end