Advertisement
aesnike

TP SCRIPT

Oct 29th, 2024 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local LocalPlayer = Players.LocalPlayer
  3. local RunService = game:GetService("RunService")
  4.  
  5. -- Variables
  6. local tpActive = false
  7. local tpLoop = nil
  8. local Character = LocalPlayer.Character
  9. local targetNPC = nil
  10. local offset = Vector3.new(0, 9, 0) -- Fixed offset above enemy
  11.  
  12. -- GUI Setup
  13. local ScreenGui = Instance.new("ScreenGui")
  14. ScreenGui.Name = "TeleportGUI"
  15. ScreenGui.ResetOnSpawn = false
  16. ScreenGui.Parent = game.CoreGui
  17.  
  18. local Button = Instance.new("TextButton")
  19. Button.Name = "TeleportButton"
  20. Button.Size = UDim2.new(0, 100, 0, 50)
  21. Button.Position = UDim2.new(1, -110, 0.5, -25)
  22. Button.AnchorPoint = Vector2.new(1, 0.5)
  23. Button.BackgroundColor3 = Color3.new(0, 0, 0)
  24. Button.TextColor3 = Color3.new(1, 1, 1)
  25. Button.Text = "TP: OFF"
  26. Button.Font = Enum.Font.SourceSansBold
  27. Button.TextSize = 18
  28. Button.Parent = ScreenGui
  29. Button.ZIndex = 9999
  30. Button.AutoButtonColor = true
  31.  
  32. -- Save/Load Functions
  33. local function saveToWorkspace()
  34. if not workspace:FindFirstChild("TeleportData") then
  35. local saveValue = Instance.new("BoolValue")
  36. saveValue.Name = "TeleportData"
  37. saveValue.Value = tpActive
  38. saveValue.Parent = workspace
  39. else
  40. workspace.TeleportData.Value = tpActive
  41. end
  42. end
  43.  
  44. local function loadFromWorkspace()
  45. if workspace:FindFirstChild("TeleportData") then
  46. return workspace.TeleportData.Value
  47. end
  48. return false
  49. end
  50.  
  51. -- Teleport Functions
  52. local function freezeCharacter()
  53. if Character and Character:FindFirstChild('Humanoid') then
  54. Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
  55. end
  56. end
  57.  
  58. local function unfreezeCharacter()
  59. if Character and Character:FindFirstChild('Humanoid') then
  60. Character.Humanoid:ChangeState(Enum.HumanoidStateType.Running)
  61. end
  62. end
  63.  
  64. local function findNearestNPC()
  65. local nearestNPC = nil
  66. local nearestDistance = math.huge
  67.  
  68. Character = LocalPlayer.Character
  69. if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
  70.  
  71. for _, folder in ipairs(workspace:GetChildren()) do
  72. if folder:IsA("Folder") then
  73. for _, child in ipairs(folder:GetChildren()) do
  74. if child:FindFirstChild("enemyFolder") then
  75. for _, npc in ipairs(child.enemyFolder:GetChildren()) do
  76. if npc:FindFirstChild("HumanoidRootPart") and
  77. npc:FindFirstChild("Humanoid") and
  78. npc.Humanoid.Health > 0 then
  79. local distance = (Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
  80. if distance < nearestDistance then
  81. nearestDistance = distance
  82. nearestNPC = npc
  83. end
  84. end
  85. end
  86. end
  87. end
  88. end
  89. end
  90. return nearestNPC
  91. end
  92.  
  93. local function stayOnTopOfTarget()
  94. if not targetNPC or not targetNPC.Parent or not targetNPC:FindFirstChild("HumanoidRootPart") then
  95. targetNPC = findNearestNPC()
  96. if not targetNPC then
  97. wait(0.1) -- Short wait if no targets found
  98. return
  99. end
  100. end
  101.  
  102. if Character and Character:FindFirstChild("HumanoidRootPart") and
  103. targetNPC and targetNPC:FindFirstChild("HumanoidRootPart") then
  104. -- Calculate fixed position above enemy
  105. local targetPos = targetNPC.HumanoidRootPart.Position + offset
  106. local targetCFrame = CFrame.new(targetPos, targetNPC.HumanoidRootPart.Position)
  107.  
  108. -- Smooth lerp to target position
  109. Character.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame:Lerp(targetCFrame, 0.5)
  110. end
  111. end
  112.  
  113. local function toggleTP(isEnabled)
  114. tpActive = isEnabled
  115. if isEnabled then
  116. freezeCharacter()
  117. if not tpLoop then
  118. tpLoop = RunService.Heartbeat:Connect(function()
  119. if tpActive then
  120. stayOnTopOfTarget()
  121. end
  122. end)
  123. end
  124. Button.BackgroundColor3 = Color3.new(0, 1, 0)
  125. Button.TextColor3 = Color3.new(0, 0, 0)
  126. Button.Text = "TP: ON"
  127. else
  128. if tpLoop then
  129. tpLoop:Disconnect()
  130. tpLoop = nil
  131. end
  132. unfreezeCharacter()
  133. Button.BackgroundColor3 = Color3.new(1, 0, 0)
  134. Button.TextColor3 = Color3.new(1, 1, 1)
  135. Button.Text = "TP: OFF"
  136. end
  137. saveToWorkspace()
  138. end
  139.  
  140. -- Button Click Handler
  141. Button.MouseButton1Click:Connect(function()
  142. toggleTP(not tpActive)
  143. end)
  144.  
  145. -- Character Added Function
  146. local function onCharacterAdded(newCharacter)
  147. Character = newCharacter
  148. wait(1)
  149. if tpActive then
  150. toggleTP(true)
  151. end
  152. end
  153.  
  154. -- Initialize
  155. LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
  156. if LocalPlayer.Character then
  157. onCharacterAdded(LocalPlayer.Character)
  158. end
  159.  
  160. -- Load saved state
  161. tpActive = loadFromWorkspace()
  162. if tpActive then
  163. toggleTP(true)
  164. end
  165.  
  166. -- Cleanup
  167. game:BindToClose(function()
  168. if tpLoop then
  169. tpLoop:Disconnect()
  170. end
  171. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement