Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to implement FindPartOnRayWithIgnoreList into this bot script?

Asked by 3 years ago
Edited 3 years ago

Hello there, scripters! I'm having a problem with implementing FindPartOnRayWithIgnoreList into my pathfinding AI. When ever I try to do it in the script it throws an error: Unable to cast value to Objects. When I put it back to FindPartOnRay in the script, it is fully working again, but there is no ignore list for the AI. Here's the code

local maxDistance = 5000

local canWander = true
local WanderX, WanderZ = 20, 20

function getHumanoid(model)
    for _, v in pairs(model:GetChildren()) do
        if v:IsA'Humanoid' then
            return v
        end
    end
end

local ai = script.Parent
local human = getHumanoid(ai)
local hroot = ai.HumanoidRootPart
local zspeed = hroot.Velocity.magnitude


local pfs = game:GetService("PathfindingService")

function GetPlayerNames()
    local players = game:GetService('Players'):GetChildren()
    local name = nil
    for _, v in pairs(players) do
        if v:IsA'Player' then
            name = tostring(v.Name)
        end
    end
    return name
end

function GetPlayersBodyParts(t)
    local torso = t
    if torso then
        local figure = torso.Parent
        for _, v in pairs(figure:GetChildren()) do
            if v:IsA'Part' then
                return v.Name
            end
        end
    else
        return "HumanoidRootPart"
    end
end

function GetTorso(part)
    local chars = game.Workspace:GetChildren()
    local torso = nil
    for _, v in pairs(chars) do
        if v:IsA'Model' and v ~= script.Parent and v.Name == GetPlayerNames() then
            local charRoot = v:FindFirstChild'HumanoidRootPart'
            if (charRoot.Position - part).magnitude < maxDistance then
                torso = charRoot
            end
        end
    end
    return torso
end

-- no touchy
local path
local waypoint
local oldpoints
local isWandering = 0

if canWander then
    spawn(function()
        while isWandering == 0 do
            isWandering = 1
            local desgx, desgz = hroot.Position.x + math.random(-WanderX, WanderX), hroot.Position.z + math.random(-WanderZ, WanderZ)
            human:MoveTo(Vector3.new(desgx, 0, desgz))
            wait(math.random(4, 6))
            isWandering = 0
        end
    end)
end

while wait() do
    local enemytorso = GetTorso(hroot.Position) 
    if enemytorso ~= nil then -- if player detected
        isWandering = 1
        local function checkw(t)
            local ci = 3
            if ci > #t then
                ci = 3
            end
            if t[ci] == nil and ci < #t then
                repeat
                    ci = ci + 1
                    wait()
                until t[ci] ~= nil
                return Vector3.new(1, 0, 0) + t[ci]
            else
                ci = 3
                return t[ci]
            end
        end

        path = pfs:CreatePath()
        path:ComputeAsync(hroot.Position, enemytorso.Position)
        waypoint = path:GetWaypoints()
        oldpoints = waypoint
        local connection;

        local direct = Vector3.FromNormalId(Enum.NormalId.Front)
        local ncf = hroot.CFrame * CFrame.new(direct)
        direct = ncf.p.unit
        local rootr = Ray.new(hroot.Position, enemytorso.Position)
        local phit, ppos = game.Workspace:FindPartOnRayWithIgnoreList(rootr, ai)

        if path and waypoint or checkw(waypoint) then
            if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
                human:MoveTo( checkw(waypoint).Position )
                human.Jump = false
            end

            if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
                wait(2)
                human.Jump = true
                wait(0.5)
                human:MoveTo( checkw(waypoint).Position )
            else
                human.Jump = false
            end

        else
            for i = 3, #oldpoints do
                human:MoveTo( oldpoints[i].Position )   
            end
        end
    elseif enemytorso == nil and canWander then -- if player not detected
        isWandering = 0
        path = nil
        waypoint = nil
        human.MoveToFinished:Wait()
    end
end

print("_char is chosen, subject is pathfinding...")

I'm pretty sure this one line breaks the whole script,

local phit, ppos = game.Workspace:FindPartOnRayWithIgnoreList(rootr, ai)

Any ideas? What is going wrong?

I've tried the workspace:Raycast but it dosen't work.

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The FindPartOnRayWithIgnoreList function is deprecated. Use workspace:Raycast() instead. More than that, the ignoreDescendantsList paramater takes a table.

0
Hello! I have tried that method, the bot moves towards the player but it does not ignore that and it will go another way which is not the fastest path? If you want you can try to implement it and see. Oliver_Bocch 32 — 3y
0
I'll try it. Oliver_Bocch 32 — 3y
0
Which filterType do I use to ignore parts? Whitelist or blacklist? Oliver_Bocch 32 — 3y
0
I have tried both filter types and have tried to add them but the ai won't ignore and go the other way. Oliver_Bocch 32 — 3y
Ad

Answer this question