HowToRoblox

GrabDetector

Jul 26th, 2021 (edited)
3,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local re = game.ReplicatedStorage:WaitForChild("OnGrabRE")
  2.  
  3.  
  4. local mouse = game.Players.LocalPlayer:GetMouse()
  5.  
  6. local cam = workspace.CurrentCamera
  7.  
  8.  
  9. local hrp = script.Parent:WaitForChild("HumanoidRootPart")
  10.  
  11.  
  12. local target = nil
  13.  
  14. local rHeld = false
  15.  
  16.  
  17. mouse.Button1Down:Connect(function()
  18.    
  19.    
  20.     local mouseTarget = mouse.Target
  21.    
  22.    
  23.     if mouseTarget and mouseTarget:FindFirstChild("CanGrab") then
  24.        
  25.        
  26.         re:FireServer(mouseTarget)
  27.        
  28.        
  29.         target = mouseTarget
  30.     end
  31. end)
  32.  
  33.  
  34. mouse.Button1Up:Connect(function()
  35.    
  36.    
  37.     re:FireServer(target, true)
  38. end)
  39.  
  40.  
  41. game:GetService("UserInputService").InputBegan:Connect(function(inp, processed)
  42.  
  43.     if not processed and inp.KeyCode == Enum.KeyCode.R and target then
  44.        
  45.         rHeld = true
  46.     end
  47. end)
  48.  
  49. game:GetService("UserInputService").InputEnded:Connect(function(inp)
  50.    
  51.     if inp.KeyCode == Enum.KeyCode.R then
  52.        
  53.         rHeld = false
  54.     end
  55. end)
  56.  
  57.  
  58. game:GetService("RunService").Heartbeat:Connect(function()
  59.    
  60.    
  61.     if target and target.CanGrab.Value == script.Parent.Name then
  62.        
  63.        
  64.         local pos = hrp.Position + (mouse.Hit.Position - hrp.Position).Unit * 15
  65.        
  66.         target.BodyPosition.Position = pos
  67.        
  68.         target.BodyGyro.CFrame = target.CFrame
  69.        
  70.        
  71.         if rHeld then
  72.            
  73.             target.BodyGyro.CFrame = target.BodyGyro.CFrame * CFrame.Angles(0, 0.1, 0)
  74.         end
  75.     end
  76. end)
Add Comment
Please, Sign In to add comment