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

How do I make this npc stop in front of the target, not into it?

Asked by
lucas4114 607 Moderation Voter
8 years ago

I have this that makes the npc walk to another npc but it just walks into it, it should walk to it but stop when it's 4 studs close to it how do I do this??

while wait() do
    local distances = {}
    local charwithdistance = {}
    for number,char in pairs (game.Workspace.NPCs:GetChildren()) do
        local distance = (char.Head.Position - script.Parent.Head.Position).magnitude
        distances[number] = distance
        charwithdistance[distance] = char
    end
    table.sort(distances)
    for number2, plr in pairs (distances) do
        local closestdistance = plr
        local closestplayer = charwithdistance[(closestdistance)]
        if closestplayer.Owner.Value ~= script.Parent.Owner.Value then
            script.Parent.Humanoid:MoveTo(closestplayer.Head.Position)
            break
        end
    end
end

I tryed doing this but it just broke the npc...

while wait() do
    local distances = {}
    local charwithdistance = {}
    for number,char in pairs (game.Workspace.NPCs:GetChildren()) do
        local distance = (char.Head.Position - script.Parent.Head.Position).magnitude
        distances[number] = distance
        charwithdistance[distance] = char
    end
    table.sort(distances)
    for number2, plr in pairs (distances) do
        local closestdistance = plr
        local closestplayer = charwithdistance[(closestdistance)]
        if closestplayer.Owner.Value ~= script.Parent.Owner.Value and closestdistance > script.Parent.GunStats.Range.Value then
            script.Parent.Humanoid:MoveTo(CFrame.new(script.Parent.Head.Position, closestplayer.Head.Position) * CFrame.new(0,0, -25))
            break
        end
    end
end

1 answer

Log in to vote
0
Answered by
Scerzy 85
8 years ago

Simply subtract a Vector value from it.

Say the target is at position (0,0,0). When you use this script, the npc walks to the location of the head of the closest player, which is (0,0,0). If you want him to stop 4 studs away, simply subtract 4 from the x or z value.

For example:

 closestplayer.Head.Position+Vector3.new(-4,0,0)
-- This gets the position of the head of the closest player and then subtracts 4 studs from the x value, so the npc will stop at this position instead.

I'm not sure exactly how you want it but you can play around with this concept of Vector adding to get the npc to stop where needed.

0
Still doesn't work it just breaks the ai now it doesn't move at all. I updated my question with what I did pls help me ;( lucas4114 607 — 8y
Ad

Answer this question