0
\$\begingroup\$

I am trying to make a maze/horror game. I used an online template in the Roblox library as my enemy. I used pathfinder as you will see in the code below. It's finding me like it's supposed to, except it only goes for my LAST position. As you can see in the image below, it completely skipped me, went to my LAST position, then started chasing me. I don't know why it only goes for my last position, and not my current position.

local PathFindingS = game:GetService("PathfindingService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
local Players = game:GetService("Players")
game.Workspace.Fruity.Humanoid.WalkSpeed = 60       


--To calculate the path
while wait() do
    
    for i, player in pairs(game.Players:GetPlayers()) do
        local character = game.Workspace:WaitForChild(player.Name)
        local characterPos = character.PrimaryPart.Position
        local path = PathFindingS:CreatePath()
        path:ComputeAsync(rootPart.Position, characterPos)
        game.Workspace.Fruity.HumanoidRootPart:SetNetworkOwner(nil) 
        
        local waypoints = path:GetWaypoints()

        for i, waypoint in pairs(waypoints) do
            local part = Instance.new("Part")
            part.Shape = "Ball"
            part.Material = "Neon"
            part.Size = Vector3.new(0.6,0.6,0.6)
            part.Position = waypoint.Position + Vector3.new(0,2,0)
            part.Anchored = true
            part.CanCollide = false
            part.Parent = game.Workspace
            humanoid:MoveTo(waypoint.Position)
            humanoid.MoveToFinished:Wait()  
        end 
    end
end

enter image description here

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

So, your code is just telling the character to move to a certain position. What you would need to do is make a loop so it runs and updates constantly.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You may include further details or some example code to support your answer, as they can make it more complete and even solve OP's problem altogether. \$\endgroup\$
    – liggiorgio
    Commented Sep 11, 2021 at 17:17
0
\$\begingroup\$

Your code tells the character to pathfind to your position and then walk there and not stop going on that path until it gets there.

Try making the character only go to the first waypoint that the pathfinding algorithm finds, then recalculating the pathfinding. This is what it would look like:

while wait() do
    for i, player in pairs(game.Players:GetPlayers()) do
        [...]
        local waypoints = path:GetWaypoints()

        for i, waypoint in pairs(waypoints) do
            local part = Instance.new("Part")
            [...] -- Do the other stuff you did to the part
            part.Parent = game.Workspace
        end
        humanoid:MoveTo(waypoints[1].Position)
        humanoid.MoveToFinished:Wait() -- Note that this approach (recalculating after every waypoint) can be very resource-intensive with many players or long distances
    end
end
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .