Attempting to get npc to jump over hazardess object

yes, i said i would look into that, but it doesnt help the current issue where the smart’s jump isnt used at all

Then improve the condition to determine when the NPC should jump.

1 Like

You can try using this script.

local function calculatedis(pos1, pos2)
    return (pos1 - pos2).magnitude
end

local function calculatedir(pos1, pos2)
    return (pos1 - pos2).unit
end

local char = script.Parent
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
local hum = script.Parent:FindFirstChildOfClass("Humanoid")
local CollectionService = game:GetService("CollectionService")

local MaxDistance = 500

local function FindNearest()
    local nearest = nil
    local nearestmag = MaxDistance
    for i, v in pairs(workspace:GetChildren()) do
        if v:FindFirstChild("HumanoidRootPart") and v:FindFirstChildOfClass("Humanoid") and v ~= char and v:FindFirstChild("Info") and v:FindFirstChild("Info").Slappable.Value == true then
            local hrp2 = v:FindFirstChild("HumanoidRootPart")
            local hum2 = v:FindFirstChildOfClass("Humanoid")
            if hum2.Health > 0 then
                local mag = calculatedis(hrp.Position, hrp2.Position)
                if mag <= nearestmag then
                    nearest = v
                    nearestmag = mag
                end
            end
        end
    end
    return nearest
end

local eInfo = {}

local function IsLocationTravelable(location)
    local rayinfo = RaycastParams.new()
    rayinfo.FilterType = Enum.RaycastFilterType.Exclude
    rayinfo.RespectCanCollide = true
    rayinfo.FilterDescendantsInstances = char:GetDescendants()
    rayinfo.CollisionGroup = "Default"

    local info = workspace:Raycast(location, Vector3.new(0, -20, 0), rayinfo)
    return info and info.Instance or false
end

local function TrailTo(Location)
    workspace.DIE:ClearAllChildren()
    local dis = calculatedis(hrp.Position, Location)
    local dir = calculatedir(Location, hrp.Position)

    local PartSeperationDistance = 1
    local VectorTable = {}
    local CanTravel = true
    for i = 0, dis, PartSeperationDistance do
        local Part = Instance.new("Part", workspace.DIE)
        Part.Anchored = true
        Part.CanCollide = false
        Part.CanQuery = false
        Part.CanTouch = false
        Part.Transparency = 0
        Part.Position = hrp.Position + (dir * i)
        Part.CFrame = CFrame.new(Part.Position, Location)
        Part.Size = Vector3.new(1, 1, 1)
        local pos = Part.Position
        local Travelable = IsLocationTravelable(pos)
        if not Travelable then
            CanTravel = false
            break
        end
        VectorTable[i] = {pos, Travelable}
    end

    return CanTravel
end

local function CanSeeTarget(Target)
    local rayinfo = RaycastParams.new()
    rayinfo.FilterType = Enum.RaycastFilterType.Exclude
    rayinfo.RespectCanCollide = true
    rayinfo.FilterDescendantsInstances = char:GetDescendants()
    rayinfo.CollisionGroup = "Default"
    local hum = Target:FindFirstChildOfClass("Humanoid")
    local ehrp = hum.RootPart

    local e = calculatedir(ehrp.Position, hrp.Position) * 100

    local Part = Instance.new("Part", workspace.DIE)
    Part.Anchored = true
    Part.CanCollide = false
    Part.CanQuery = false
    Part.CanTouch = false
    Part.Transparency = 0
    Part.Position = hrp.Position + e
    Part.CFrame = CFrame.new(Part.Position, ehrp.Position)
    Part.Color = Color3.new(0, 1, 0)
    Part.Size = Vector3.new(1, 1, 1)

    local info = workspace:Raycast(hrp.Position, e, rayinfo)
    return info and info.Instance:IsDescendantOf(Target)
end

local function PathfindToLocation(Location, eChar)
    workspace.PathfindDebug:ClearAllChildren()
    if not IsLocationTravelable(Location) then return false end

    local agentParams = {
        ["AgentHeight"] = 5.5,
        ["AgentRadius"] = 4,
        ["AgentCanJump"] = true,
        ["Costs"] = {
            Hazard = math.huge
        }
    }
    local PathfindingService = game:GetService("PathfindingService")
    local Path = PathfindingService:CreatePath(agentParams)
    Path:ComputeAsync(hrp.Position, Location)
    local Waypoints = Path:GetWaypoints()
    for _, v in pairs(Waypoints) do
        local Part = Instance.new("Part", workspace.PathfindDebug)
        Part.Anchored = true
        Part.CanCollide = false
        Part.CanQuery = false
        Part.CanTouch = false
        Part.Transparency = 0
        Part.Position = v.Position
        Part.Size = Vector3.new(1, 1, 1)
        Part.Color = Color3.new(1, 0, 0)
    end

    for _, v in pairs(Waypoints) do
        local pos = v.Position
        if not IsLocationTravelable(pos) then
            return
        end

        hum:MoveTo(pos)
        if v.Action == Enum.PathWaypointAction.Jump then
            hum.Jump = true
        end

        repeat
            task.wait()
        until hum.WalkToPoint == pos or hum:GetState() == Enum.HumanoidStateType.Jumping
    end
end

local BaseRange = 6
local function NearestHazardessObject()
    local hazards = {}
    for _, object in pairs(workspace:GetDescendants()) do
        if CollectionService:HasTag(object, "Hazard") and (object.Position - hrp.Position).Magnitude < BaseRange then
            table.insert(hazards, object)
            return true, hazards
        end
    end
    return false
end

local function Smart(eChar)
    local ehrp = eChar:FindFirstChild("HumanoidRootPart")
    local ehum = eChar:FindFirstChildOfClass("Humanoid")
    local Distance = calculatedis(hrp.Position, ehrp.Position)
    local Tool = char:FindFirstChildOfClass("Tool")

    if Distance <= 8 then
        Tool:Activate()
    end

    local CanTravel = TrailTo(ehrp.Position)
    local CanSee = CanSeeTarget(eChar)

    if Distance >= 50 and Distance <= 100 then
        if CanTravel and CanSee then
            hum:MoveTo(ehrp.Position)
        else
            PathfindToLocation(ehrp.Position, eChar)
        end
    elseif Distance < 50 and Distance > 25 then
        if CanTravel and CanSee then
            hum:MoveTo(ehrp.Position)
        else
            PathfindToLocation(ehrp.Position, eChar)
        end
        --I added jump logic.
        if ehum.FloorMaterial == Enum.Material.Air and hum:GetState() ~= Enum.HumanoidStateType.Jumping then
            local Chance = math.random(1, 5)
            if Chance == 1 and not eChar:FindFirstChild("Ragdolled") then
                hum.Jump = true
            end
        end
    elseif Distance < 25 and Distance > 10 then
        local Chance = math.random(1, 25)
        if Chance > 1 then
            if CanTravel and CanSee then
                hum:MoveTo(ehrp.Position)
            else
                PathfindToLocation(ehrp.Position, eChar)
            end
        else
            hum.Jump = true
        end
    end
end

local CanEvade = true
while task.wait() do
    local nearest = FindNearest()
    if nearest then
        Smart(nearest)
    end
end