olivia1246

Roblox Follow LocalScript

Jul 22nd, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | Gaming | 0 0
  1. local Players = game:GetService("Players")
  2. local localPlayer = Players.LocalPlayer
  3. local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
  4. local humanoid = character:WaitForChild("Humanoid")
  5. local rootPart = character:WaitForChild("HumanoidRootPart")
  6.  
  7. local function findNearestRootPart(pos)
  8.     local nearest = nil
  9.     local minDist = math.huge
  10.    
  11.     for _, player in ipairs(Players:GetPlayers()) do
  12.         if player ~= localPlayer then
  13.             local otherCharacter = player.Character
  14.             if otherCharacter then
  15.                 local otherRootPart = otherCharacter:FindFirstChild("HumanoidRootPart")
  16.                 local otherHumanoid = otherCharacter:FindFirstChild("Humanoid")
  17.                
  18.                 if otherRootPart and otherHumanoid and otherHumanoid.Health > 0 then
  19.                     local dist = (otherRootPart.Position - pos).Magnitude
  20.                     if dist < minDist then
  21.                         nearest = otherRootPart
  22.                         minDist = dist
  23.                     end
  24.                 end
  25.             end
  26.         end
  27.     end
  28.    
  29.     return nearest
  30. end
  31.  
  32. -- Function to make the character jump
  33. local function jump()
  34.     humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  35. end
  36.  
  37. -- Coroutine for random jumping
  38. local function randomJumpCoroutine()
  39.     while true do
  40.         local waitTime = math.random(1, 5)
  41.         task.wait(waitTime)
  42.         jump()
  43.     end
  44. end
  45.  
  46. -- Start the random jumping coroutine
  47. task.spawn(randomJumpCoroutine)
  48.  
  49. while true do
  50.     local target = findNearestRootPart(rootPart.Position)
  51.     if target then
  52.         humanoid:MoveTo(target.Position)
  53.     end
  54.     task.wait(0.1)
  55. end
Advertisement
Add Comment
Please, Sign In to add comment