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

How would I get my pathfinding AI to follow a player's position?

Asked by 4 years ago

I've been working on pathfinding ai, and I know how to get an ai to walk around randomly and from one point to another, but I just haven't been able to figure out how to get a player's position so the ai follows them. I haven't been able to get the value out of event functions that get the players position. How would I return the value so I could use it as the end goal of the move function? Here's my code. Any help is appreciated :)

function move()
    local human = script.Parent.Humanoid
    local torso = script.Parent.HumanoidRootPart
    local service = game:GetService("PathfindingService")
    script.Parent.HumanoidRootPart.Anchored = false

    local path = service:CreatePath()
    path:ComputeAsync(torso.Position,game.ReplicatedStorage.Vector3Value.Value) -- DONT KNOW HOW TO GET PLAYER POSITION
    local waypoints = path:GetWaypoints()

-- getting player position  
    game.Players.PlayerAdded:Connect(function(player)
        game.Players.CharacterAdded:Connect(function(character)
            local value = character.HumanoidRootPart.Position
            game.ReplicatedStorage.Vector3Value.Value = Vector3.new(character.HumanoidRootPart.Position) -- DONT KNOW HOW TO GET PLAYERS POSITION OUT OF THIS FUNCTION
        end)
    end)    

    for i, v in pairs(waypoints) do
        human:MoveTo(v.Position)
        human.MoveToFinished:Wait(2)
    end
end

while wait(3) do
    move()
end

1 answer

Log in to vote
-1
Answered by
ImTrev 344 Moderation Voter
4 years ago
Edited 4 years ago
local RS = game:GetService("RunService")
local RepS = game:GetService("ReplicatedStorage")
local NPC = workspace.NPC.Humanoid

game.Players.PlayerAdded:Connect(function(plr)

    plr.CharacterAdded:Wait()

    local HRP = plr.Character.HumanoidRootPart

    local lastPos = HRP.Position

    RS.Stepped:connect(function()
        if lastPos ~= HRP.Position then
            lastPos = HRP.Position
            NPC:MoveTo(HRP.Position)
        end

    end)        

end)

I made this hope it helps. If you need any more help please contact me. I'm not sure this is 100% what you're looking for though.

Ad

Answer this question