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

"Position is not a valid member of Vector3" what's wrong with path?

Asked by 2 years ago
Edited 2 years ago

Hello, I'm tried to make a pathfinding for my npc, but something isn't working, I tried to find the path but... "Position is not a valid member of Vector3", please how do I fix this? My script is below

local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Neck")
local lowerTorso = script.Parent:WaitForChild("Torso")

local clone = script.Parent:Clone()

function findPath(target)
    local path = game:GetService("PathfindingService"):CreatePath()
    path:ComputeAsync(myRoot.Position,target.Position)
    local waypoints = path:GetWaypoints()

    if path.Status == Enum.PathStatus.Success then
        for _, waypoint in ipairs(waypoints) do
            myHuman:MoveTo(waypoint.Position)
            local timeOut = myHuman.MoveToFinished:Wait(1)
            if not timeOut then
                print("Path too long!")
                findPath(target)
                break
            end
            if checkSight(target) then
                repeat
                    print("Moving directly to the target")
                    myHuman:MoveTo(target.Position)
                    attack(target)
                    wait(0.05)
                    if target == nil then
                        break
                    elseif target.Parent == nil then
                        break
                    end
                until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
                break
            end
            if (myRoot.Position - waypoints[1].Position).magnitude > 20 then
                print("Target has moved, generating new path")
                findPath(target)
                break
            end
        end
    end
end

function checkSight(target)
    local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 40)
    local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
    if hit then
        if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - myRoot.Position.Y) < 3 then
            print("I can see the target")
            return true
        end
    end
    return false
end

function findTarget()
    local dist = 50
    local target = nil
    local potentialTargets = {}
    local seeTargets = {}
    for i,v in ipairs(workspace:GetChildren()) do
        local human = v:FindFirstChild("Humanoid")
        local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
        if human and torso and v.Name ~= script.Parent.Name then
            if (myRoot.Position - torso.Position).magnitude < dist and human.Health > 0 then
                table.insert(potentialTargets,torso)
            end
        end
    end
    if #potentialTargets > 0 then
        for i,v in ipairs(potentialTargets) do
            if checkSight(v) then
                table.insert(seeTargets, v)
            elseif #seeTargets == 0 and (myRoot.Position - v.Position).magnitude < dist then
                target = v
                dist = (myRoot.Position - v.Position).magnitude
            end
        end
    end
    if #seeTargets > 0 then
        dist = 99999
        for i,v in ipairs(seeTargets) do
            if (myRoot.Position - v.Position).magnitude < dist then
                target = v
                dist = (myRoot.Position - v.Position).magnitude
            end
        end
    end
    return target
end

function attack(target)
    if (myRoot.Position - target.Position).magnitude < 5 then
        if target.Parent ~= nil then
            target.Parent.Humanoid:TakeDamage(100)
        end
        wait(0.25)
    end
end

function died()
    wait(5)
    clone.Parent = workspace
    game:GetService("Debris"):AddItem(script.Parent,0.1)
end

myHuman.Died:Connect(died)

lowerTorso.Touched:Connect(function(obj)
    if not obj.Parent:FindFirstChild("Humanoid") then
        myHuman.Jump = true
    end
end)

function APatf(destination)
    local PS = game:GetService("PathfindingService")

    local pathParams = {
        ["AgentHeight"] = 6.625,
        ["AgentRadius"] = 2,
        ["AgentCanJump"] = false
    }

    local path = PS:CreatePath(pathParams)

    path:ComputeAsync(myRoot.Position,destination.Position)

    return path
end

function walkRandomly(destination)

    local path = APatf(destination)
    local waypoints = path:GetWaypoints()

    if path.Status == Enum.PathStatus.Success then
        for _, waypoint in ipairs(waypoints) do
            if waypoint.Action == Enum.PathWaypointAction.Jump then
                myHuman.Jump = false
            end
            myHuman:MoveTo(waypoint.Position)
            local timeOut = myHuman.MoveToFinished:Wait(1)
            if not timeOut then
                print("Got stuck")
                myHuman.Jump = false
                walkRandomly()
            end
        end
    else
        print("Path failed")
        wait(1)
        walkRandomly()
    end
end


function main()
    local target = findTarget()
    local Animation = script:WaitForChild("Animation")
    local Spotting = script:WaitForChild("Spotting")
    local humanoid = script.Parent:WaitForChild("Humanoid")
    local spot = humanoid:LoadAnimation(Spotting)
    local Running = humanoid:LoadAnimation(Animation)
    local song = workspace.ChasingTheme
    local chasing = 0
    if target then
        chasing = 1
        myHuman.WalkSpeed = 0
        if song.IsPlaying == false then
            song:Play()
            spot:Play()
            spot.Stopped:Wait()
        end
        myHuman.WalkSpeed = 12
        findPath(target)
        Running:Play()
    else
        chasing = 0
        myHuman.WalkSpeed = 3
        song:Stop()
        walkRandomly(workspace.destination.Position)
        walkRandomly(workspace.destination2.Position)
    end
end

while wait(0.1) do
    if myHuman.Health < 1 then
        break
    end
    main()
end


and error

1 answer

Log in to vote
0
Answered by
boredlake 256 Moderation Voter
2 years ago

Well, I'm assuming one of your variables is already a Vector3 which means it is already a position in world space so you do not need to index it with .Position. In the error message it should specify a line number, so remove the .Position in that line.

0
No way, BRO YOU GOT BIG BRAIN! R0bloxian_Gaming 4 — 2y
Ad

Answer this question