Advertisement
Mokiros

ball

Apr 18th, 2021
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. local Tool = Instance.new("Tool")
  2. Tool.Name = "Ball"
  3. Tool.RequiresHandle = false
  4. Tool.CanBeDropped = false
  5.  
  6. Tool.Parent = owner.Backpack
  7. local ls = NLS([[
  8. local Tool = script:WaitForChild("ToolPointer").Value
  9. local Ball = Tool:WaitForChild("Ball")
  10. local Weld,Humanoid
  11. local RS = game:GetService("RunService")
  12.  
  13. local ControlModule = require(game:GetService("Players").LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")).controls
  14.  
  15. local cam = workspace.CurrentCamera
  16. workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
  17.     cam = workspace.CurrentCamera
  18. end)
  19.  
  20. local CameraAngleUpdateConnection
  21. local AnimPlayedConnection
  22. local bf = Instance.new("BodyForce")
  23. bf.Parent = Ball
  24.  
  25. local function calculateRawMoveVector(cameraRelativeMoveVector)
  26.     if not cam then
  27.         return cameraRelativeMoveVector
  28.     end
  29.  
  30.     local c, s
  31.     local _, _, _, R00, R01, R02, _, _, R12, _, _, R22 = cam.CFrame:GetComponents()
  32.     if R12 < 1 and R12 > -1 then
  33.         -- X and Z components from back vector.
  34.         c = R22
  35.         s = R02
  36.     else
  37.         -- In this case the camera is looking straight up or straight down.
  38.         -- Use X components from right and up vectors.
  39.         c = R00
  40.         s = -R01*math.sign(R12)
  41.     end
  42.     local norm = math.sqrt(c*c + s*s)
  43.     return Vector3.new(
  44.         (c*cameraRelativeMoveVector.x + s*cameraRelativeMoveVector.z)/norm,
  45.         0,
  46.         (c*cameraRelativeMoveVector.z - s*cameraRelativeMoveVector.x)/norm
  47.     )
  48. end
  49.  
  50. local force = 10000
  51. local jumped = false
  52.  
  53. local function UpdateCameraAngle()
  54.     if not ControlModule.activeController then
  55.         bf.Force = Vector3.new(0,0,0)
  56.     end
  57.     local MoveVector = ControlModule.activeController:GetMoveVector()
  58.     local CameraRelative = ControlModule.activeController:IsMoveVectorCameraRelative()
  59.     local isJumping = ControlModule.activeController:GetIsJumping()
  60.     if not jumped and isJumping then
  61.         jumped = true
  62.         Ball:ApplyImpulse(Vector3.new(0,10000,0))
  63.     else
  64.         jumped = false
  65.     end
  66.     if CameraRelative then
  67.         MoveVector = calculateRawMoveVector(MoveVector)
  68.     end
  69.     bf.Force = MoveVector * force
  70. end
  71.  
  72. local function AnimationPlayed(anim)
  73.     anim:Stop()
  74. end
  75.  
  76. local function UnequipTool()
  77.     if Weld then
  78.         Weld:Destroy()
  79.         Weld = nil
  80.     end
  81.     if Humanoid then
  82.         Humanoid:ChangeState(Enum.HumanoidStateType.FallingDown)
  83.         Humanoid = nil
  84.     end
  85.     if CameraAngleUpdateConnection then
  86.         CameraAngleUpdateConnection:Disconnect()
  87.         CameraAngleUpdateConnection = nil
  88.     end
  89.     if AnimPlayedConnection then
  90.         AnimPlayedConnection:Disconnect()
  91.         AnimPlayedConnection = nil
  92.     end
  93. end
  94.  
  95. local function EquipTool()
  96.     local Character = Tool.Parent
  97.     local HRP = Character:FindFirstChild("HumanoidRootPart")
  98.     local hum = Character:FindFirstChild("Humanoid")
  99.     if not HRP or not hum then return end
  100.     Ball.CFrame = HRP.CFrame
  101.     Ball.AssemblyLinearVelocity = HRP.AssemblyLinearVelocity
  102.     Ball.AssemblyAngularVelocity = HRP.AssemblyAngularVelocity
  103.     Weld = Instance.new("Weld")
  104.     Weld.Part0 = HRP
  105.     Weld.Part1 = Ball
  106.     Weld.Parent = Ball
  107.    
  108.     CameraAngleUpdateConnection = RS.Heartbeat:Connect(UpdateCameraAngle)
  109.    
  110.     Humanoid = hum
  111.     Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
  112.    
  113.     wait()
  114.     local animator = Humanoid
  115.     for _,anim in pairs(animator:GetPlayingAnimationTracks()) do
  116.         anim:Stop(0.1)
  117.     end
  118.     AnimPlayedConnection = animator.AnimationPlayed:Connect(AnimationPlayed)
  119. end
  120.  
  121. Tool.Equipped:Connect(EquipTool)
  122. Tool.Unequipped:Connect(UnequipTool)
  123.  
  124. ]],owner.PlayerGui)
  125. local ptr = Instance.new("ObjectValue")
  126. ptr.Name = "ToolPointer"
  127. ptr.Value = Tool
  128. ptr.Parent = ls
  129.  
  130. local Ball = Instance.new("Part")
  131. Ball.Material = Enum.Material.SmoothPlastic
  132. Ball.Transparency = 0.75
  133. Ball.Shape = Enum.PartType.Ball
  134. Ball.Size = Vector3.new(8,8,8)
  135. Ball.Name = "Ball"
  136. Ball.Parent = Tool
  137.  
  138. local Players = game:GetService("Players")
  139.  
  140. Tool.Equipped:Connect(function()
  141.     local Character = Tool.Parent
  142.     local Player = Players:GetPlayerFromCharacter(Character)
  143.     if not Player then return end
  144.     Ball:SetNetworkOwner(Player)
  145. end)
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement