Facebook
From Smelly Kitten, 3 Years ago, written in Lua.
This paste is a reply to Code from Saylac - view diff
Embed
Download Paste or View Raw
Hits: 391
  1. local neck = workspace.Dummy.Head.Neck
  2. local NPC = workspace.Dummy
  3.  
  4. function getClosestPlayer()
  5.         local closest_player, closest_distance = nil, 30
  6.         for i, player in pairs(workspace:GetChildren()) do
  7.                 if player:FindFirstChild("Humanoid") and player ~= NPC then
  8.                         local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
  9.                         if distance < closest_distance then
  10.                                 closest_player = player
  11.                                 closest_distance = distance
  12.                         end
  13.                 end
  14.         end
  15.         return closest_player
  16. end
  17.  
  18. local cframe0 = neck.C0
  19. while true do
  20.         local player = getClosestPlayer()
  21.         if player then
  22.                 local is_in_front = NPC.PrimaryPart.CFrame:ToObjectSpace(player.PrimaryPart.CFrame).Z < 0
  23.                 if is_in_front then
  24.                         local unit = -(NPC.PrimaryPart.CFrame.p - player.PrimaryPart.Position).unit
  25.                         neck.C0 = cframe0 * CFrame.new(Vector3.new(0, 0, 0), unit)
  26.                 end
  27.         end
  28.         wait()
  29. end