Advertisement
aesnike

scrtp Dungeon quest

Oct 21st, 2024 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local player = Players.LocalPlayer
  3. local character = player.Character or player.CharacterAdded:Wait()
  4. local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  5. local humanoid = character:WaitForChild("Humanoid")
  6. local debounce = false
  7.  
  8. local teleportRange = 15 -- Distance to teleport away from dangerous objects
  9. local debounceTime = 0.1 -- Time delay to prevent rapid teleporting
  10.  
  11. -- Function to calculate a safe position to teleport to
  12. local function getSafeTeleportPosition(dangerousObject)
  13. -- Get the direction from the dangerous object to the player
  14. local direction = (humanoidRootPart.Position - dangerousObject.Position).Unit
  15.  
  16. -- Calculate a new position that's teleportRange studs away from the dangerous object
  17. local safePosition = humanoidRootPart.Position + direction * teleportRange
  18.  
  19. -- Return the safe position
  20. return safePosition
  21. end
  22.  
  23. -- Function to teleport the player to a safe position
  24. local function teleportAway(dangerousObject)
  25. -- Get the safe teleport position
  26. local safePosition = getSafeTeleportPosition(dangerousObject)
  27.  
  28. -- Teleport the player's HumanoidRootPart to the safe position
  29. humanoidRootPart.CFrame = CFrame.new(safePosition)
  30. end
  31.  
  32. -- Function to determine if a part is dangerous based on certain criteria
  33. local function isDangerousPart(part)
  34. local partName = part.Name:lower()
  35. -- Check if the part's name contains 'precast', 'hitbox', or other known danger indicators
  36. return part:IsA("BasePart") and (partName:find("precast") or partName:find("hitbox") or partName:find("danger"))
  37. end
  38.  
  39. -- Function to continuously detect dangerous objects and teleport the player
  40. local function detectAndTeleport()
  41. if debounce then return end
  42. debounce = true
  43.  
  44. -- Look for dangerous parts in the workspace
  45. for _, obj in pairs(workspace:GetDescendants()) do
  46. -- Check if it's a dangerous part
  47. if isDangerousPart(obj) then
  48. local distance = (humanoidRootPart.Position - obj.Position).Magnitude
  49.  
  50. -- If within range, teleport away from the dangerous object
  51. if distance <= teleportRange then
  52. teleportAway(obj)
  53. break -- Exit after teleporting away from one object
  54. end
  55. end
  56. end
  57.  
  58. debounce = false
  59. task.wait(debounceTime)
  60. end
  61.  
  62. -- Function to set up the teleport detection for the current character
  63. local function setupTeleportDetection()
  64. while true do
  65. detectAndTeleport()
  66. task.wait(0.05) -- Frequent checks for dangerous objects
  67. end
  68. end
  69.  
  70. -- Function to reconnect the dodge mechanism when the player respawns
  71. local function onCharacterAdded(newCharacter)
  72. character = newCharacter
  73. humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  74. humanoid = character:WaitForChild("Humanoid")
  75.  
  76. task.spawn(setupTeleportDetection) -- Start detecting and teleporting again after respawn
  77. end
  78.  
  79. -- Initial setup for the current character
  80. task.spawn(setupTeleportDetection)
  81.  
  82. -- Reapply the dodge logic when the player respawns
  83. player.CharacterAdded:Connect(onCharacterAdded)
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement