Advertisement
TheRealAaron

Body control

Jan 5th, 2025
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1.  
  2.  
  3. To achieve what you’re asking for—making a character's right leg (R6) follow the mouse cursor in a game—you can use **Roblox Lua scripting** within Roblox Studio. Here's how you can get started:
  4.  
  5. ---
  6.  
  7. ### **Steps:**
  8.  
  9. 1. **Open Roblox Studio** and create a new project or load an existing one.
  10. 2. Ensure your character is in R6 mode.
  11. 3. Insert a **LocalScript** into the StarterPlayerScripts.
  12.  
  13. ---
  14.  
  15. ### **Script Example:**
  16. ```lua
  17. local player = game.Players.LocalPlayer
  18. local mouse = player:GetMouse()
  19. local character = player.Character or player.CharacterAdded:Wait()
  20. local rightLeg = character:WaitForChild("Right Leg") -- Make sure it's R6
  21. local runService = game:GetService("RunService")
  22.  
  23. local function updateLegPosition()
  24.     if mouse.Target then
  25.         local mousePosition = mouse.Hit.p -- Get mouse position in the world
  26.         local legPosition = rightLeg.Position
  27.  
  28.         -- Calculate direction vector to move the leg
  29.         local direction = (mousePosition - legPosition).unit
  30.         local newPosition = legPosition + direction * 0.5 -- Adjust speed
  31.  
  32.         -- Update leg's position while holding the right mouse button
  33.         rightLeg.CFrame = CFrame.new(newPosition, mousePosition)
  34.     end
  35. end
  36.  
  37. mouse.Button2Down:Connect(function()
  38.     runService.RenderStepped:Connect(updateLegPosition) -- Update every frame
  39. end)
  40.  
  41. mouse.Button2Up:Connect(function()
  42.     runService.RenderStepped:Disconnect(updateLegPosition)
  43. end)
  44. ```
  45.  
  46. ---
  47.  
  48. ### **Explanation:**
  49.  
  50. 1. **Mouse Tracking:**  
  51.    - `mouse.Hit.p` gets the mouse's position in 3D space.
  52.  
  53. 2. **Leg Movement:**  
  54.   - Adjust the `CFrame` of the right leg to follow the cursor while ensuring it stays attached to the character's body.
  55.  
  56. 3. **Holding Right Mouse Button:**  
  57.    - `mouse.Button2Down` starts tracking the mouse.
  58.    - `mouse.Button2Up` stops tracking.
  59.  
  60. 4. **Performance:**  
  61.    - `RenderStepped` ensures smooth updates synchronized with the frame rate.
  62.  
  63. ---
  64.  
  65. ### **Tips for Testing:**
  66. - Make sure the script is in **StarterPlayerScripts**.
  67. - Test in **Play Mode** in Roblox Studio.
  68. - Adjust the movement speed or constraints if necessary.
  69.  
  70. If you encounter any issues, let me know!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement