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

So Path.Blocked doesnt work... Is there a way around it?

Asked by 5 years ago
Edited 5 years ago

So I was working with the pathfinding service and everything was going fine... until I realized that Path.Blocked apparently doesn't work correctly! As a result, I've been trying to find a way around this. I came up with a solution of maybe making lines through the waypoints and checking for a touched event or something? I'm not sure how I'd do that though. If you have any ideas I'm all ears!

Here is my code - it needs to create a new path if a part blocks the path....

001--Pahfinding service
002local pathfinding = game:GetService("PathfindingService")
003--Settings for the path
004local obHorSep = 3
005local obVerSep = 5
006--Variables for storing the waypoints and the current waypoint index
007local waypoints
008local curWayIndex
009--Variables for npc, npc humanoid, the object, and the destination.
010local npc
011local npcHum
012local part
013local destination
014--Stores if the npc is moving
015local moving = false
View all 105 lines...

Oh also I read about this... https://devforum.roblox.com/t/pathfindingservice-path-blocked-not-firing-why/278379 They seemed to have the same problem as me. There was a kind of solution at the end, but I'm not sure how I'd even do that!

I tried to change it to:

01if path.Status == Enum.PathStatus.Success then
02        waypoints = path:GetWaypoints() --Gets waypoints
03        --Sets the index to the start
04        curWayIndex = 1
05        --Starts movement
06        moving = true
07        --START CHECKING FOR VELOCITY CHANGE
08        npcHum.RootPart:GetPropertyChangedSignal('Velocity'):Connect(velocityChanged)
09        npcHum:MoveTo(waypoints[curWayIndex].Position)
10        --After first movement, the moveToFinish will complete the rest
11    else

adding this to it:

1--START CHECKING FOR VELOCITY CHANGE
2npcHum.RootPart:GetPropertyChangedSignal('Velocity'):Connect(velocityChanged)

and then I added this function:

1--Checks if velocity has gone below 0.1 (not moving)
2function velocityChanged()
3    print("Changed")
4    if(npcHum.RootPart.Velocity.magnitude < 0.1) then
5        moving = false
6        createPath()
7    end
8end

Since if it did fire, it would create an infinite loop and moved the connection to here:

01for _,v in pairs(npc:GetDescendants()) do
02        --Checks if descendant is humanoid
03        if v:IsA("Humanoid") then
04            --Sets the public npcHum value to the humanoid
05            npcHum = v
06            --Connects moveToFinish to npcHum so it reached next waypoint.
07            npcHum.MoveToFinished:Connect(moveToFinish)
08            --START CHECKING FOR VELOCITY CHANGE
09            npcHum.RootPart:GetPropertyChangedSignal('Velocity'):Connect(velocityChanged)
10            break
11        end
12    end

But it still won't work?

2 answers

Log in to vote
1
Answered by
sheepposu 561 Moderation Voter
5 years ago

I was reading at the end of the article, someone said to check the velocity. You can check it this way object.Velocity.magnitude where object is the object that you want the velocity of. You can check if it is less than 0.1 and make a new path if so, quoted from the article.

0
Is there a way to connect this to a changed event or something? Adv3rtizement 101 — 5y
0
object:GetPropertyChangedSignal('Velocity'):Connect(function() sheepposu 561 — 5y
0
    if part.Velocity.Magnitude then sheepposu 561 — 5y
0
        --code sheepposu 561 — 5y
View all comments (20 more)
0
Didn't work, my attempted code has been edited into the post. Adv3rtizement 101 — 5y
0
You could try checking for a change in the velocities magnitude sheepposu 561 — 5y
0
GetPropertyChangedSignal is not a valid member of Vector3 Adv3rtizement 101 — 5y
0
Thought that might happen sheepposu 561 — 5y
0
Is the velocity changing in the first place sheepposu 561 — 5y
0
Well the npc is moving Adv3rtizement 101 — 5y
0
Everything works fine except for the fact that it doesn't change it's pathway. Adv3rtizement 101 — 5y
0
Does the npc ever stop moving, or is it not moving in the first place sheepposu 561 — 5y
0
I did a test... it prints out different velocities. Adv3rtizement 101 — 5y
0
If I don't mess with anything, it will move all the way to the end without stopping. If I put a wall in the path while it is moving it will walk into the wall and stop Adv3rtizement 101 — 5y
0
hmm... then the event should go off. I also noticed something, the GetPropertyChangedSignal should only be executed once. sheepposu 561 — 5y
0
Otherwise it will start a bunch of events which can eventually crash the game sheepposu 561 — 5y
0
How come? Adv3rtizement 101 — 5y
0
Everytime you create a new path, it would create a new event (event will constantly check), after accumulating so many events running it will most likely crash sheepposu 561 — 5y
0
Ok, I'll move it to another then Adv3rtizement 101 — 5y
0
I changed it - look at the post Adv3rtizement 101 — 5y
0
I think it would work better if you just put it by itself, for example, directly after the velocityChanged function. sheepposu 561 — 5y
0
After you do that, test it out and see if the event is firing sheepposu 561 — 5y
0
npcHum isn't set uptil the setValues function Adv3rtizement 101 — 5y
0
You can put it after the setValues function sheepposu 561 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Well, apparently a while loop works too :P - Not laggy

001--Pahfinding service
002local pathfinding = game:GetService("PathfindingService")
003--Settings for the path
004local obHorSep = 3
005local obVerSep = 5
006--Variables for storing the waypoints and the current waypoint index
007local waypoints
008local curWayIndex
009--Variables for npc, npc humanoid, the object, and the destination.
010local npc
011local npcHum
012local part
013local destination
014--Stores if the npc is moving
015local moving = false
View all 110 lines...

Answer this question