Advertisement
AALTTz

zen's button run

Jun 16th, 2023 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. -- zen made dis pls give me credit
  2. -- i added some notes just in case you got confused, also you can change the default playerspeed when the player is done running
  3. -- change the button size, color, and image to whatever you want.
  4.  
  5. -- Get the ImageButton
  6. local button = script.Parent
  7.  
  8. -- Get the UserInputService
  9. local userInputService = game:GetService("UserInputService")
  10.  
  11. -- Check if the platform is mobile
  12. local isMobile = userInputService.TouchEnabled
  13.  
  14. -- Hide the button if the platform is not mobile
  15. if not isMobile then
  16. button.Visible = false
  17. end
  18.  
  19. -- Variables for the hold-to-run animation
  20. local holdToRunEnabled = false
  21. local holdToRunStartTime = 0
  22. local holdToRunThreshold = 0.5 -- Adjust this value to change the hold time required to trigger the animation
  23.  
  24. -- Define the desired speed when the button is pressed
  25. local runSpeed = 32 -- Adjust this value to change the running speed
  26.  
  27. -- Define the animation ID for the running animation
  28. local runAnimationId = "rbxassetid://13774142136" -- Replace with the actual animation ID
  29.  
  30. -- Store the running animation track
  31. local runAnimationTrack
  32. local playerspeed = 16 -- Change this to your default game speed, 16 is the normal. - zen
  33.  
  34. -- Function to handle button press and release events
  35. local function onButtonInput(inputObject)
  36. local player = game.Players.LocalPlayer
  37. local character = player.Character
  38. if character then
  39. local humanoid = character:FindFirstChildOfClass("Humanoid")
  40. if isMobile then
  41. -- On mobile, use TouchStarted and TouchEnded events
  42. if inputObject.UserInputType == Enum.UserInputType.Touch then
  43. if inputObject.UserInputState == Enum.UserInputState.Begin then
  44. holdToRunEnabled = true
  45. holdToRunStartTime = tick()
  46. humanoid.WalkSpeed = runSpeed
  47. if humanoid:GetState() == Enum.HumanoidStateType.Running then
  48. if not runAnimationTrack then
  49. -- Load and play the running animation
  50. local animation = Instance.new("Animation")
  51. animation.AnimationId = runAnimationId
  52. runAnimationTrack = humanoid:LoadAnimation(animation)
  53. runAnimationTrack:Play()
  54. else
  55. -- Resume the running animation if it was previously paused
  56. runAnimationTrack:AdjustSpeed(runSpeed / humanoid.WalkSpeed)
  57. runAnimationTrack:Play()
  58. end
  59. end
  60. elseif inputObject.UserInputState == Enum.UserInputState.End and holdToRunEnabled then
  61. holdToRunEnabled = false
  62. humanoid.WalkSpeed = playerspeed -- Restore default WalkSpeed
  63.  
  64. -- Pause the running animation
  65. if runAnimationTrack then
  66. runAnimationTrack:AdjustSpeed(1)
  67. runAnimationTrack:Stop()
  68. end
  69. end
  70. end
  71. else
  72. -- On other platforms, use MouseButton1 event
  73. if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
  74. if inputObject.UserInputState == Enum.UserInputState.Begin then
  75. holdToRunEnabled = true
  76. holdToRunStartTime = tick()
  77. humanoid.WalkSpeed = runSpeed
  78. if humanoid:GetState() == Enum.HumanoidStateType.Running then
  79. if not runAnimationTrack then
  80. -- Load and play the running animation
  81. local animation = Instance.new("Animation")
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement