WalkToPoint is acting funny

The code below is a hacky pathfinding system I created for furniture. It uses WalkToPoint and magnitude to determine a players position relative to specific nodes, which are what most of the variables are. (R1, R2, L1, etc)

WalkToPoint works perfectly for the complicated paths, say if you were behind the chair, you would walk to R2 (Right side of the chair), then to R1 (Right diagonal of front/side) then to the target (Front of the chair).

However, it doesn’t work for every path. In fact, the only path it doesn’t want to complete is the simplest one. Just from where the player is to the target, no other points between them.

It does work a little bit, meaning the very first time you activate said simplest path, it works. Any times after that, and it doesn’t want to follow. It still works for the other paths though.

Everything was going smoothly, until I hit this problem, which uses the exact same commands as the other paths. I have no clue what the problem could be.

-KeysOfFate, a non-forum member.

Chair = script.Parent

Model = Chair.Parent

L1 = Model.L1
L2 = Model.L2

R1 = Model.R1
R2 = Model.R2

Rear = Model.Rear
Target = Model.Target

X1 = nil
X2 = nil

function IsNotInBack()
if (Torso.Position - X2.Position).magnitude < (Torso.Position - X1.Position).magnitude then

    Torso.Parent.Humanoid.WalkToPoint = X1.Position

    print("Going to X1!")

    repeat
        wait(0.05)
    until (Torso.Position - X1.Position).magnitude <= 4

    Torso.Parent.Humanoid.WalkToPoint = Target.Position

    repeat
        wait(0.05)
    until (Torso.Position - Target.Position).magnitude <= 4

    print("Pathfinding successful!")


else
    print("Attempting to go to target point")


    Torso.Parent.Humanoid.WalkToPoint = Target.Position


    repeat
        wait(0.05)
    until (Torso.Position - Target.Position).magnitude <= 4

    print("Pathfinding successful!")

end

end
function IsInBack()
if (Torso.Position - X2.Position).magnitude > (Torso.Position - Rear.Position).magnitude then
print(“Player is in the rear!”)
Torso.Parent.Humanoid.WalkToPoint = X2.Position

    print("Going to X2!")

    repeat
        wait(0.05)
    until (Torso.Position - X2.Position).magnitude <= 4


    Torso.Parent.Humanoid.WalkToPoint = X1.Position

    print("Going to X1!")

    repeat
        wait(0.05)
    until (Torso.Position - X1.Position).magnitude <= 4


    Torso.Parent.Humanoid.WalkToPoint = Target.Position

    print("Going to Target!")

    repeat
        wait(0.05)
    until (Torso.Position - Torso.Position).magnitude <= 4

    print("Pathfinding successful!")



else print("Player is not in the rear!")
    IsNotInBack()
end

end
Chair.ClickDetector.MouseClick:Connect(function(Player)
Torso = Player.Character.Torso

    print("Beginning pathfinding sequence!")

    if (Torso.Position - R2.Position).magnitude < (Torso.Position - L2.Position).magnitude then
        print("Player is on the right side!")
        X1 = R1
        X2 = R2
        IsInBack() 
    else 
        print("Player is on the left side!")
        X1 = L1
        X2 = L2
        IsInBack()

end -- IF STATEMENT #1 FINISH

end)

Try using Humanoid:MoveTo(point.Position).
I’ve found out that if you try to give an equal target position with WalkToPoint that it won’t do anything.

3 Likes

Supreme Leader KeysOfFate, wishes to inform you many thanks for this post.

3 Likes