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
9 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??

01while wait() do
02    local distances = {}
03    local charwithdistance = {}
04    for number,char in pairs (game.Workspace.NPCs:GetChildren()) do
05        local distance = (char.Head.Position - script.Parent.Head.Position).magnitude
06        distances[number] = distance
07        charwithdistance[distance] = char
08    end
09    table.sort(distances)
10    for number2, plr in pairs (distances) do
11        local closestdistance = plr
12        local closestplayer = charwithdistance[(closestdistance)]
13        if closestplayer.Owner.Value ~= script.Parent.Owner.Value then
14            script.Parent.Humanoid:MoveTo(closestplayer.Head.Position)
15            break
16        end
17    end
18end

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

01while wait() do
02    local distances = {}
03    local charwithdistance = {}
04    for number,char in pairs (game.Workspace.NPCs:GetChildren()) do
05        local distance = (char.Head.Position - script.Parent.Head.Position).magnitude
06        distances[number] = distance
07        charwithdistance[distance] = char
08    end
09    table.sort(distances)
10    for number2, plr in pairs (distances) do
11        local closestdistance = plr
12        local closestplayer = charwithdistance[(closestdistance)]
13        if closestplayer.Owner.Value ~= script.Parent.Owner.Value and closestdistance > script.Parent.GunStats.Range.Value then
14            script.Parent.Humanoid:MoveTo(CFrame.new(script.Parent.Head.Position, closestplayer.Head.Position) * CFrame.new(0,0, -25))
15            break
16        end
17    end
18end

1 answer

Log in to vote
0
Answered by
Scerzy 85
9 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:

1closestplayer.Head.Position+Vector3.new(-4,0,0)
2-- 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 — 9y
Ad

Answer this question