I am making a pathfinding zombie but I need it to update the path when the player it's following moves. However, the event connecting to the function never fires, doesn't give an output, and won't break if I put a breakpoint at that line.
This first part of the code runs fine:
players = game.Players:GetPlayers()
randomPlayer = players[math.random(1, #players)]
print(randomPlayer.name)
pathService = game:GetService("PathfindingService")
The broken-looking code is here, and it'd be helpful to know what went wrong:
game.Workspace:WaitForChild(randomPlayer).Character.Humanoid.Running:connect(function(speed)
print("Moved")
game.Workspace.Points:ClearAllChildren()
wait(0.1)
rawPath = pathService:ComputeRawPathAsync(script.Parent.Torso.Position, randomPlayer.Character.HumanoidRootPart.Position, 512)
path = rawPath:GetPointCoordinates()
end)
I will also post the entire script (which isn't much) in case the problem doesn't lie in the above snippet:
wait(0.1)
while true do
players = game.Players:GetPlayers()
randomPlayer = players[math.random(1, #players)]
print(randomPlayer.name)
pathService = game:GetService("PathfindingService")
wait(0.1)
rawPath = pathService:ComputeRawPathAsync(script.Parent.Torso.Position, randomPlayer.Character.HumanoidRootPart.Position, 512)
path = rawPath:GetPointCoordinates()
game.Workspace.Points:ClearAllChildren()
partReached = true
for x = 1, #path do
if partReached == true then
partReached = false
p = Instance.new("Part", game.Workspace.Points)
p.CanCollide = false
p.FormFactor = Enum.FormFactor.Symmetric
p.Size = Vector3.new(1,1,1)
repeat wait() until path[x] ~= nil
p.Position = path[x]
p.Anchored = true
p.BrickColor = BrickColor.DarkGray()
p.Transparency = 0
script.Parent.MazeHumanoid:MoveTo(p.Position, p)
end
while partReached == false do
wait(0.1)
p.Touched:connect(function(part)
if part.Parent.MazeHumanoid ~= nil or part.Parent.Parent.MazeHumanoid ~= nil then
partReached = true
end
end)
end
end
end
game.Workspace:WaitForChild(randomPlayer).Character.Humanoid.Running:connect(function(speed)
print("Moved")
game.Workspace.Points:ClearAllChildren()
wait(0.1)
rawPath = pathService:ComputeRawPathAsync(script.Parent.Torso.Position, randomPlayer.Character.HumanoidRootPart.Position, 512)
path = rawPath:GetPointCoordinates()
end)
