New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
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 49
3 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
royaltoe 4391 Moderation Voter Community Moderator
3 months ago
Edited 3 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