Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to fire a certain function whenever you arrive to your destination with pathfindingservice?

Asked by
Derzett 51
11 months ago

Hello, so i want a GUI to activate whenever i arrive at a certain point i selected with pathfindingservice but i couldn't find an event to fire whenever that happened. For example something like pathfindingservice.Finished:Connect(function(). Thanks!

1 answer

Log in to vote
1
Answered by
n7e 5093 Moderation Voter Community Moderator
11 months ago
Edited 11 months ago

What you're trying to do is described here.

I'm not going to post the script here, because it's a bit long, but how it works is computes a path for a player/npc to follow in order to reach a certain destination.

Visually, it looks like this:

!visual

Every time you reach one of those dots/waypoints, the onWaypointReached function runs.

This is our 'onWaypointReached' function:

local function onWaypointReached(reached)
    if reached and currentWaypointIndex < #waypoints then
        currentWaypointIndex = currentWaypointIndex + 1
        humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
    end
end

What you have to do from there is check if the waypoint that the player most recently reached is the last waypoint by saying:

local function onWaypointReached(reached)
    if reached and currentWaypointIndex < #waypoints then
        currentWaypointIndex = currentWaypointIndex + 1
        humanoid:MoveTo(waypoints[currentWaypointIndex].Position)

    --add this bit to the onWaypointReached function
    elseif(currentWaypointIndex == #waypoints)then
        --the player reached the final waypoint, do something!
    end
end
Ad

Answer this question