So what I want to do is make a smart killing npc that can go through doors and follow the player without jumping into a wall for the whole game.
I have everything down except how to make the npc focus on a player not a block or another NPC
First we need to find the nearest person.
Example:
01 | local vecPoint = workspace.NPC.Humanoid.Position |
02 | local closestPlayer, smallestMagnitude = nil , math.huge |
03 |
04 | for _, player in pairs (game.Players:GetPlayers()) do |
05 | local distance = player:DistanceFromCharacter(vecPoint) |
06 | if distance < smallestMagnitude then |
07 | smallestMagnitude = distance |
08 | closestPlayer = player |
09 | end |
10 | end |
Boom, we did it. Now we want the player to go the Player.
Script Example:
1 | NPC.Humanoid:MoveTo(player.Character.Humanoid) |
Final script:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | player.CharacterAdded:connect( function (character) |
03 | if character then |
04 | local function movetonpc() |
05 | local vecPoint = workspace.NPC.Humanoid.Position |
06 | local closestPlayer, smallestMagnitude = nil , math.huge |
07 |
08 | for _, player in pairs (game.Players:GetPlayers()) do |
09 | local distance = player:DistanceFromCharacter(vecPoint) |
10 | if distance < smallestMagnitude then |
11 | smallestMagnitude = distance |
12 | closestPlayer = player |
13 |
14 | NPC.Humanoid:MoveTo(player.Character.Humanoid) |
15 | end |