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

Why is Humanoid:MoveTo not functioning correctly?

Asked by 4 years ago
Edited 4 years ago

I'm trying to make a smart NPC system in which enemies will try to move around the character, so what I've done is made them move randomly in front of my character, however, they all seem to act as if I'm using Model:MoveTo instead of Humanoid:MoveTo

TravelTarget = function(npc,dist,rot)
    local target = npc.Data.Target.Value
    local h = npc.Humanoid
    local nc = CFrame.new(
        npc.HumanoidRootPart.CFrame.Position,
        target.CFrame.Position
    )
    local rc = nc - nc.Position
    local x,y,z = rc:toOrientation()
        npc.HumanoidRootPart.CFrame = nc * CFrame.Angles(-x,0,-z)

    rot = math.random(unpack(rot)) or math.random(-90,90)
    local location = (
        (target.CFrame * CFrame.Angles(0,math.rad(rot),0)) * CFrame.new(0,0,-dist)
    )
    h:MoveTo(location.Position)
    wait(0.2)
    h:MoveTo(npc.HumanoidRootPart.CFrame.Position)
    h.MoveToFinished:Wait()
end

Visualization: https://gyazo.com/6e345fa5c606d1fca808860db1c6e6c6

I can confirm that the issue isn't ping or server lag as well.

0
I can't see the gif. rabbi99 714 — 4y
0
It looks like you have too many NPCs trying to go to the same spot, so either increase their radius, if you're using the pathfinding service, or have them go to a spot near but not directly on top of each other. SteamG00B 1633 — 4y
0
It still happens when I have one NPC. No PFS by the way. zboi082007 270 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Figured it out on my own, I had to rotate them using a BodyGyro instead of directly changing the CFrame of the character.

0
Why CFrame.Position? Just keep CFrame, WideSteal321 773 — 4y
Ad
Log in to vote
-2
Answered by 4 years ago

Because you forgot to put the "part". Do this:

TravelTarget = function(npc,dist,rot)
    local target = npc.Data.Target.Value
    local h = npc.Humanoid
    local nc = CFrame.new(
        npc.HumanoidRootPart.CFrame.Position,
        target.CFrame.Position
    )
    local rc = nc - nc.Position
    local x,y,z = rc:toOrientation()
        npc.HumanoidRootPart.CFrame = nc * CFrame.Angles(-x,0,-z)

    rot = math.random(unpack(rot)) or math.random(-90,90)
    local location = (
        (target.CFrame * CFrame.Angles(0,math.rad(rot),0)) * CFrame.new(0,0,-dist)
    )
    h:MoveTo(location.Position, location)
    wait(0.2)
    h:MoveTo(npc.HumanoidRootPart.CFrame.Position, npc.HumanoidRootPart)
    h.MoveToFinished:Wait()
end
0
The part parameter isn't necessary, besides, that brings an undesired effect. zboi082007 270 — 4y

Answer this question