ShoccessX

looker

Sep 9th, 2024 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. local Players = game:GetService('Players')
  2. local Workspace = game:GetService('Workspace')
  3.  
  4. local player = Players.LocalPlayer
  5. local character = player.Character or player.CharacterAdded:Wait()
  6. local humanoid = character:WaitForChild("Humanoid")
  7.  
  8. local positions = {
  9. Vector3.new(21, 4, -433),
  10. Vector3.new(-47, 4, -434),
  11. Vector3.new(21, 4, -408),
  12. Vector3.new(-47, 4, -409)
  13. }
  14.  
  15. local function walkToNearestPosition()
  16. if game.PlaceId ~= 14269621394 then
  17. return
  18. end
  19.  
  20. local playerPosition = character.HumanoidRootPart.Position
  21. local nearestPosition = nil
  22. local minDistance = math.huge
  23.  
  24. for _, pos in pairs(positions) do
  25. local distance = (pos - playerPosition).Magnitude
  26. if distance < minDistance then
  27. nearestPosition = pos
  28. minDistance = distance
  29. end
  30. end
  31.  
  32. if nearestPosition then
  33. print("Moving to nearest position at: " .. tostring(nearestPosition))
  34. humanoid:MoveTo(nearestPosition)
  35.  
  36. local moveToConnection
  37. moveToConnection = humanoid.MoveToFinished:Connect(function(reached)
  38. if reached then
  39. print("Player has reached the position!")
  40. moveToConnection:Disconnect()
  41. else
  42. print("Failed to reach the position. Trying again...")
  43. humanoid:MoveTo(nearestPosition)
  44. end
  45. end)
  46. else
  47. print("No valid positions found.")
  48. end
  49. end
  50.  
  51. -- Call the function to move to the nearest position
  52. walkToNearestPosition()
Advertisement
Add Comment
Please, Sign In to add comment