Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. -- Variables
  2. local doubleJumpManager = {Connections = {}}
  3. local coreModule = require(script:FindFirstAncestor("CoreModule"))
  4. local config = require(script.Config)
  5.  
  6. -- Initialize
  7. function doubleJumpManager:Initialize(character, humanoid)
  8.     local currentNumberOfDoubleJumps = 0
  9.     local maxNumberOfDoubleJumps = 10
  10.    
  11.     -- JumpRequest (but actually InputBegan because JumpRequest doesn't work good)
  12.     doubleJumpManager.Connections.JumpRequest = coreModule.Services.UserInputService.InputBegan:Connect(function(input, isGameProcessedEvent)
  13.         if input.KeyCode ~= Enum.KeyCode.Space or isGameProcessedEvent then return end
  14.         if humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then return end
  15.         if currentNumberOfDoubleJumps == maxNumberOfDoubleJumps then return end
  16.        
  17.         -- Jump time
  18.         humanoid.JumpPower = config.DefaultJumpPower*config.DoubleJumpBoostMultiplier
  19.         humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  20.         currentNumberOfDoubleJumps = currentNumberOfDoubleJumps + 1
  21.     end)
  22.        
  23.     -- StateChanged
  24.     doubleJumpManager.Connections.StateChanged = humanoid.StateChanged:Connect(function(oldHumanoidState, newHumanoidState)
  25.         if newHumanoidState == Enum.HumanoidStateType.Landed then
  26.             humanoid.JumpPower = config.DefaultJumpPower
  27.             currentNumberOfDoubleJumps = 0
  28.        
  29.         -- Falling to your death by physics prevention
  30.         elseif newHumanoidState == Enum.HumanoidStateType.Freefall then
  31.             while humanoid:GetState() == Enum.HumanoidStateType.Freefall do
  32.                 local primaryPart = character.PrimaryPart
  33.                
  34.                 -- You're in danger
  35.                 if primaryPart and primaryPart.Velocity.Magnitude >= config.HighFallingVelocityDangerPoint then
  36.                     local _, position = workspace:FindPartOnRay(Ray.new(character.PrimaryPart.Position, Vector3.new(0, -1, 0)*50), character)
  37.                    
  38.                     -- You're gonna die
  39.                     if (primaryPart.Position - position).Magnitude <= config.HighFallingVelocityInterventionDistance then
  40.                         character.PrimaryPart.Velocity = Vector3.new(0, 0, 0)
  41.                         require(script.HighVelocityInterventionAnimation)(character)
  42.                     end
  43.                 end
  44.                
  45.                 wait()
  46.             end
  47.         end
  48.     end)       
  49. end
  50.  
  51. -- Methods
  52. function doubleJumpManager:DisconnectConnections()
  53.     for _, connection in next, doubleJumpManager.Connections do
  54.         connection:Disconnect()
  55.     end
  56.    
  57.     doubleJumpManager.Connections = {}
  58. end
  59.  
  60. --
  61. return doubleJumpManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement