Advertisement
aesnike

TP??

Oct 30th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local RunService = game:GetService("RunService")
  3. local LocalPlayer = Players.LocalPlayer
  4.  
  5. -- Function to find the nearest NPC (as provided)
  6. local function findNearestNPC()
  7. local nearestNPC = nil
  8. local nearestDistance = math.huge
  9.  
  10. local Character = LocalPlayer.Character
  11. if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
  12.  
  13. local dungeon = workspace:FindFirstChild("dungeon")
  14. if not dungeon then return end
  15.  
  16. for _, child in ipairs(dungeon:GetChildren()) do
  17. if child:FindFirstChild("enemyFolder") then
  18. for _, npc in ipairs(child.enemyFolder:GetChildren()) do
  19. if npc:FindFirstChild("HumanoidRootPart") and npc:FindFirstChild("Humanoid") and npc.Humanoid.Health > 0 then
  20. local distance = (Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
  21. if distance < nearestDistance then
  22. nearestDistance = distance
  23. nearestNPC = npc
  24. end
  25. end
  26. end
  27. end
  28. end
  29. return nearestNPC
  30. end
  31.  
  32. local function softLockCharacter(character, position)
  33. if character and character:FindFirstChild("HumanoidRootPart") then
  34. local connection
  35. connection = RunService.Heartbeat:Connect(function()
  36. if character.Parent and character:FindFirstChild("HumanoidRootPart") then
  37. character.HumanoidRootPart.CFrame = position
  38. else
  39. connection:Disconnect()
  40. end
  41. end)
  42. return connection
  43. end
  44. end
  45.  
  46. local softLockConnection
  47.  
  48. local function teleportToNPC()
  49. local Character = LocalPlayer.Character
  50. if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
  51.  
  52. local npc = findNearestNPC()
  53. if npc and npc:FindFirstChild("HumanoidRootPart") then
  54. local npcPosition = npc.HumanoidRootPart.Position
  55. local teleportPosition = Vector3.new(npcPosition.X, npcPosition.Y + 9, npcPosition.Z)
  56.  
  57. -- Calculate the direction to face the NPC
  58. local lookVector = (npcPosition - teleportPosition).Unit
  59. local lookCFrame = CFrame.new(teleportPosition, teleportPosition + lookVector)
  60.  
  61. Character.HumanoidRootPart.CFrame = lookCFrame
  62.  
  63. -- Disconnect previous soft-lock if exists
  64. if softLockConnection then
  65. softLockConnection:Disconnect()
  66. end
  67.  
  68. -- Apply new soft-lock
  69. softLockConnection = softLockCharacter(Character, lookCFrame)
  70. end
  71. end
  72.  
  73. -- Main loop
  74. local function mainLoop()
  75. while true do
  76. teleportToNPC()
  77. wait(2) -- Wait for 2 seconds before the next teleportation
  78. end
  79. end
  80.  
  81. -- Function to setup character
  82. local function setupCharacter(character)
  83. character:WaitForChild("HumanoidRootPart")
  84. coroutine.wrap(mainLoop)()
  85. end
  86.  
  87. -- Initial setup
  88. if LocalPlayer.Character then
  89. setupCharacter(LocalPlayer.Character)
  90. end
  91.  
  92. -- Handle character respawns
  93. LocalPlayer.CharacterAdded:Connect(setupCharacter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement