Guest User

Untitled

a guest
Apr 18th, 2025
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1.  
  2. local Myself:Player? = game:GetService("Players").LocalPlayer
  3. local MyMouse:Mouse = Myself:GetMouse()
  4. local MyCharacter:Model = script.Parent
  5. local MyCamera:Camera = workspace.CurrentCamera
  6.  
  7.  
  8. local UserInput:UserInputService = game:GetService("UserInputService")
  9. local RunService:RunService = game:GetService("RunService")
  10. local Storage = game:GetService('ReplicatedStorage')
  11.  
  12.  
  13. local Keycodes = Enum.KeyCode
  14. local InputTypes = Enum.UserInputType
  15.  
  16.  
  17. local ControllerBindings = {ButtonR2 = "Grab", ButtonL1 = "ZoomIn", ButtonR1 = "ZoomOut"}
  18. local KeyboardBindings = { MouseButton1 = "Grab", Q = "ZoomIn", E = "ZoomOut"}
  19. local BindingTypes = {GamePad1 = ControllerBindings,MouseButton1 = KeyboardBindings,MouseButton2 = KeyboardBindings,Keyboard = KeyboardBindings}
  20. local DirectionToCF = {Up = CFrame.Angles(0,math.rad(100),0),Down=CFrame.Angles(0,math.rad(-100),0)}
  21.  
  22.  
  23. local RayParams:RaycastParams = RaycastParams.new()
  24. RayParams.RespectCanCollide = true
  25. RayParams.FilterDescendantsInstances = {MyCharacter}
  26.  
  27.  
  28. local GrabOrientation:AlignOrientation = script.AlignOrientation
  29. local GrabPosition:AlignPosition = script.AlignPosition
  30. local GrabAttachment:Attachment
  31. local GrabDetails = {}
  32. local Zooming = 0
  33. local Turning:CFrame = CFrame.Angles(0,0,0)
  34. local ZoomSpeed = 5
  35. local MaxDistance = 8
  36. local DistanceBuffer = 4
  37. ---------------------
  38.  
  39. local Checker = require(Storage.Modules.GrabbableChecker)
  40. local PlateHandler = require(Storage.Modules.PlateHandler)
  41.  
  42.  
  43. function Drop()
  44. GrabDetails["Part"],GrabDetails["Distance"] = nil
  45. GrabPosition.Attachment0,GrabOrientation.Attachment0 = nil
  46. GrabAttachment:Destroy()
  47. Storage.Events.Release:FireServer()
  48. end
  49. function Rotate(Up:number)
  50. if GrabDetails["Orientation"] then
  51. if UserInput:IsKeyDown(Enum.KeyCode.LeftControl) then
  52. GrabDetails["Orientation"] *= CFrame.Angles(math.rad(10 * Up),0,0)
  53. else
  54. GrabDetails["Orientation"] *= CFrame.Angles(0,math.rad(10 * Up),0)
  55. end
  56. end
  57. end
  58. MyMouse.WheelForward:Connect(function() Rotate(1) end)
  59. MyMouse.WheelBackward:Connect(function() Rotate(-1) end)
  60.  
  61. UserInput.InputBegan:Connect(function(input,InGUI)
  62. if not InGUI then
  63. local KeyCode = input.KeyCode.Name
  64. local InputType = input.UserInputType.Name
  65. local Bindings = BindingTypes[InputType]
  66. if Bindings then Action = Bindings[InputType] or Bindings[KeyCode] else return; end
  67.  
  68. if Action == "Grab" then
  69. local CameraCF:CFrame = MyCamera.CFrame
  70. local GrabRay:RaycastResult = workspace:Raycast(CameraCF.Position,CameraCF.LookVector.Unit * MaxDistance,RayParams)
  71.  
  72. if GrabRay then
  73. local Target:Part = GrabRay.Instance
  74. Checker.Check(Target)
  75. if Checker.Grabbable then
  76. PlateHandler.DetachFromPlate(Target)
  77.  
  78. GrabDetails["Part"], GrabDetails["Distance"] = Target, GrabRay.Distance
  79.  
  80.  
  81. local GrabOffset = Target:GetAttribute("GrabOffset")
  82.  
  83. GrabDetails["Part"],GrabDetails["Distance"] = Target,GrabRay.Distance
  84. local Offset:CFrame = Target:GetAttribute("GrabOffset")
  85. if not Offset then
  86. Offset = CFrame.new(0,0,0)
  87. GrabDetails["Orientation"] = CameraCF:ToObjectSpace(Target.CFrame)
  88. else
  89. GrabDetails["Orientation"] = Offset
  90. end
  91.  
  92. GrabAttachment = Instance.new("Attachment")
  93. GrabAttachment.Parent = Target
  94. GrabAttachment.WorldPosition = GrabRay.Position
  95. GrabPosition.Attachment0,GrabOrientation.Attachment0 = GrabAttachment,GrabAttachment
  96. GrabAttachment.Destroying:Connect(Drop)
  97.  
  98. Storage.Events.Grab:FireServer(Target)
  99. end
  100. end
  101. elseif Action == "ZoomIn" then Zooming = -1
  102. elseif Action == "ZoomOut" then Zooming = 1
  103. end
  104. end
  105. end)
  106.  
  107. UserInput.InputEnded:Connect(function(input)
  108. local KeyCode = input.KeyCode.Name
  109. local InputType = input.UserInputType.Name
  110. local Bindings = BindingTypes[InputType]
  111.  
  112. if Bindings then Action = Bindings[KeyCode] or Bindings[InputType]
  113. else return end
  114.  
  115. if Action == "Grab" and GrabDetails["Part"] then Drop()
  116. elseif Action == "ZoomIn" or Action == "ZoomOut" then Zooming = 0
  117. elseif DirectionToCF[Action] then Turning = CFrame.Angles(0,0,0) end
  118. end)
  119.  
  120. RunService.Heartbeat:Connect(function(delta)
  121. local CameraCF:CFrame = MyCamera.CFrame
  122. if GrabDetails["Distance"] then
  123. GrabDetails["Distance"] = math.clamp(GrabDetails["Distance"] + Zooming * ZoomSpeed * delta,1,MaxDistance)
  124. GrabPosition.Position = CameraCF.Position + CameraCF.LookVector.Unit * GrabDetails["Distance"]
  125. GrabOrientation.CFrame = CameraCF:ToWorldSpace(GrabDetails["Orientation"])
  126.  
  127. if (GrabDetails["Part"].Position - CameraCF.Position).Magnitude > MaxDistance + DistanceBuffer then
  128. Drop()
  129. end
  130. end
  131. end)
  132.  
  133.  
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment