Advertisement
Thesxctriper23214

Basic script

May 28th, 2025
1,250
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. --[[
  2. Sprint Script
  3. Features:
  4. • Smooth acceleration/deceleration for walk ↔ sprint transitions
  5. • Stamina management with depletion and regeneration
  6. • Subtle camera roll (tilt) on sprint using local forward‐axis rotation
  7. • Configurable parameters for speed, rates, and tilt
  8. ]]
  9.  
  10. local Players          = game:GetService("Players")
  11. local RunService       = game:GetService("RunService")
  12. local UserInputService = game:GetService("UserInputService")
  13.  
  14. local player    = Players.LocalPlayer
  15. local camera    = workspace.CurrentCamera
  16. local character, humanoid, rootPart
  17.  
  18. -- Configuration
  19. local walkSpeed    = 16
  20. local sprintSpeed  = 32
  21. local accelRate    = 50       -- studs/s²
  22. local decelRate    = 70       -- studs/s²
  23. local maxStamina   = 5        -- seconds
  24. local staminaDrain = 1        -- per second
  25. local staminaRegen = 0.5      -- per second
  26. local tiltAngle    = 5        -- degrees of roll
  27. local tiltSpeed    = 10       -- interpolation speed
  28.  
  29. -- State
  30. local currentSpeed = walkSpeed
  31. local isSprinting  = false
  32. local stamina      = maxStamina
  33. local currentRoll  = 0        -- radians
  34.  
  35. -- Handle respawns
  36. local function onCharacterAdded(char)
  37.     character = char
  38.     humanoid   = char:WaitForChild("Humanoid")
  39.     rootPart   = char:WaitForChild("HumanoidRootPart")
  40.    
  41.     -- reset state
  42.     currentSpeed = walkSpeed
  43.     isSprinting  = false
  44.     stamina      = maxStamina
  45.     currentRoll  = 0
  46. end
  47.  
  48. player.CharacterAdded:Connect(onCharacterAdded)
  49. if player.Character then
  50.     onCharacterAdded(player.Character)
  51. end
  52.  
  53. -- Detect sprint input
  54. UserInputService.InputBegan:Connect(function(input, processed)
  55.     if not processed and input.KeyCode == Enum.KeyCode.LeftShift then
  56.         isSprinting = true
  57.     end
  58. end)
  59.  
  60. UserInputService.InputEnded:Connect(function(input, processed)
  61.     if not processed and input.KeyCode == Enum.KeyCode.LeftShift then
  62.         isSprinting = false
  63.     end
  64. end)
  65.  
  66. -- Main update loop
  67. RunService.RenderStepped:Connect(function(dt)
  68.     if not humanoid then return end
  69.  
  70.     local moving = humanoid.MoveDirection.Magnitude > 0
  71.  
  72.     -- Determine target speed
  73.     local targetSpeed = (isSprinting and moving and stamina > 0)
  74.         and sprintSpeed
  75.         or walkSpeed
  76.  
  77.     -- Smooth acceleration/deceleration
  78.     if currentSpeed < targetSpeed then
  79.         currentSpeed = math.min(currentSpeed + accelRate * dt, targetSpeed)
  80.     elseif currentSpeed > targetSpeed then
  81.         currentSpeed = math.max(currentSpeed - decelRate * dt, targetSpeed)
  82.     end
  83.     humanoid.WalkSpeed = currentSpeed
  84.  
  85.     -- Stamina logic
  86.     if isSprinting and moving and stamina > 0 then
  87.         stamina = math.max(stamina - staminaDrain * dt, 0)
  88.         if stamina == 0 then
  89.             isSprinting = false
  90.         end
  91.     else
  92.         stamina = math.min(stamina + staminaRegen * dt, maxStamina)
  93.     end
  94.  
  95.     -- Improved camera roll tilt
  96.     local targetRoll = (currentSpeed > walkSpeed + 0.1)
  97.         and math.rad(tiltAngle)
  98.         or 0
  99.     local alpha     = math.clamp(tiltSpeed * dt, 0, 1)
  100.     local deltaRoll = (targetRoll - currentRoll) * alpha
  101.  
  102.     if math.abs(deltaRoll) > 1e-4 then
  103.         -- Rotate camera around its local forward axis
  104.         camera.CFrame = camera.CFrame * CFrame.Angles(0, 0, deltaRoll)
  105.         currentRoll = currentRoll + deltaRoll
  106.     end
  107. end)
  108.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement