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....
002 | local pathfinding = game:GetService( "PathfindingService" ) |
019 | local path = pathfinding:CreatePath( { AgentRadius = obHorSep,AgentHeight = obVerSep } ) |
021 | path:ComputeAsync(npcHum.RootPart.Position,destination) |
022 | path:ComputeAsync(npcHum.RootPart.Position,destination) |
024 | if path.Status = = Enum.PathStatus.Success then |
025 | waypoints = path:GetWaypoints() |
030 | npcHum:MoveTo(waypoints [ curWayIndex ] .Position) |
036 | path.Blocked:Connect(pathBlocked) |
037 | local color = BrickColor.Random() |
038 | for _, v in pairs (waypoints) do |
039 | local ball = Instance.new( "Part" ,workspace) |
040 | ball.Size = Vector 3. new( 1 , 1 , 1 ) |
041 | ball.BrickColor = color |
042 | ball.Shape = Enum.PartType.Ball |
043 | ball.Material = Enum.Material.Neon |
045 | ball.Position = v.Position |
046 | ball.CanCollide = false |
050 | function setValues(npc 1 ,part 1 ) |
055 | for _,v in pairs (npc:GetDescendants()) do |
057 | if v:IsA( "Humanoid" ) then |
061 | npcHum.MoveToFinished:Connect(moveToFinish) |
066 | if part:IsA( "Model" ) then |
067 | if part.PrimaryPart then |
068 | destination = part.PrimaryPart.position |
071 | destination = part:FindFirstChildOfClass( "Part" ).position |
075 | destination = part.Position |
080 | function pathBlocked(blockIdx) |
081 | print ( "Oh no! Blockage in the path!" ) |
083 | if blockIdx > curWayIndex then |
084 | print ( "The blockage is infront, we need to make a new path!" ) |
089 | print ( "Dont worry, the blockage is behind!" ) |
093 | function moveToFinish(reached) |
095 | if reached and curWayIndex < #waypoints then |
097 | curWayIndex = curWayIndex+ 1 |
099 | npcHum:MoveTo(waypoints [ curWayIndex ] .Position) |
100 | elseif curWayIndex = = #waypoints then |
102 | print ( "Point Reached!" ) |
105 | setValues(script.Parent,workspace.Test.PartSpecial) |
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:
01 | if path.Status = = Enum.PathStatus.Success then |
02 | waypoints = path:GetWaypoints() |
08 | npcHum.RootPart:GetPropertyChangedSignal( 'Velocity' ):Connect(velocityChanged) |
09 | npcHum:MoveTo(waypoints [ curWayIndex ] .Position) |
adding this to it:
2 | npcHum.RootPart:GetPropertyChangedSignal( 'Velocity' ):Connect(velocityChanged) |
and then I added this function:
2 | function velocityChanged() |
4 | if (npcHum.RootPart.Velocity.magnitude < 0.1 ) then |
Since if it did fire, it would create an infinite loop and moved the connection to here:
01 | for _,v in pairs (npc:GetDescendants()) do |
03 | if v:IsA( "Humanoid" ) then |
07 | npcHum.MoveToFinished:Connect(moveToFinish) |
09 | npcHum.RootPart:GetPropertyChangedSignal( 'Velocity' ):Connect(velocityChanged) |
But it still won't work?