world_killer

Untitled

Jan 14th, 2022
907
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --services
  2. local work = game:GetService("Workspace")
  3. local uis = game:GetService("UserInputService")
  4.  
  5. --player
  6. local player = game:GetService("Players").LocalPlayer
  7. local char = player.CharacterAdded:Wait()
  8.  
  9. local delta
  10. local cam = work.CurrentCamera
  11.  
  12. local rayParams = RaycastParams.new()
  13. rayParams.FilterType = Enum.RaycastFilterType.Blacklist
  14. rayParams.FilterDescendantsInstances = {char}
  15.  
  16. local objectsToLift = {}
  17.  
  18. work.DescendantAdded:Connect(function(part)
  19.     if not part:IsA("BasePart") then return end
  20.     if part.Anchored then return end
  21.    
  22.     local size = part.Size
  23.     if size.X + size.Y + size.Z > 50 then return end
  24.    
  25.     table.insert(objectsToLift, part)
  26. end)
  27.  
  28. for _, part in ipairs(work:GetDescendants()) do
  29.     if not part:IsA("BasePart") then continue end
  30.     if part.Anchored then continue end
  31.  
  32.     local size = part.Size
  33.     if size.X + size.Y + size.Z > 50 then continue end
  34.  
  35.     table.insert(objectsToLift, part)
  36. end
  37.  
  38. local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
  39. local pressingRightMiddleFinger = false
  40. local pressingLeftMiddleFinger = false
  41.  
  42. local head = char:WaitForChild("Head")
  43. local rightHand = char:WaitForChild("RightHand"):WaitForChild("Main")
  44.  
  45. --objects and if they are lifting
  46. local rightHolding = nil
  47. local rightLifting = false
  48.  
  49. local rightObjectDistToHand = nil
  50. local dist = nil
  51.  
  52. local function FindPartToLift()
  53.     local angle = -rightHand.CFrame.RightVector:Dot((head.Position - rightHand.Position).Unit)
  54.    
  55.     if angle > -.5 then--pointing too much towards themself
  56.         if rightHolding then
  57.             rightHolding.ParticleEmitter:Destroy()
  58.             remote:FireServer("Drop", rightHolding)
  59.             rightHolding = nil
  60.         end
  61.         return
  62.     end
  63.    
  64.     local closest = 0
  65.     local temp = nil
  66.    
  67.     for _, part in ipairs(objectsToLift) do
  68.  
  69.         --check for interfering objects
  70.         local partFound = work:Raycast(rightHand.Position, part.Position - rightHand.position, rayParams)
  71.         if not partFound then continue end
  72.         if partFound.Instance ~= part then continue end
  73.  
  74.         --get unit vectors and check dot product
  75.         local vec1 = (part.Position - rightHand.Position).Unit
  76.         local vec2 = rightHand.CFrame.RightVector *-1
  77.  
  78.         local angle = (math.acos(vec1:Dot(vec2)) * 180/math.pi)
  79.        
  80.         if angle > 15 or angle < closest then
  81.             continue
  82.         end
  83.        
  84.         closest = angle
  85.         temp = part
  86.     end
  87.    
  88.     --set up new part with particle
  89.     if temp and temp ~= rightHolding then
  90.         rightHolding = temp
  91.        
  92.         local particle = Instance.new("ParticleEmitter")
  93.         particle.Speed = NumberRange.new(10)
  94.         particle.Size = NumberSequence.new(1)
  95.         particle.Lifetime = NumberSequenceKeypoint.new(.1,.1,.1)
  96.         particle.Color = ColorSequence.new(Color3.fromRGB(159, 243, 233))
  97.         particle.Parent = rightHolding
  98.     end
  99.    
  100.     --remove particle from old chosen part
  101.     for _, part in ipairs(objectsToLift) do
  102.         if part == rightHolding or not part:FindFirstChild("ParticleEmitter") then continue end
  103.         part.ParticleEmitter:Destroy()
  104.     end
  105. end
  106.  
  107.  
  108. --my own input began/input ended method for vr as roblox's version is slow
  109. uis.InputChanged:Connect(function(input)
  110.     --right hand starting/stopping pressing
  111.     if input.KeyCode == Enum.KeyCode.ButtonR1 then
  112.         if input.Position.Z >= .8 and not pressingRightMiddleFinger then
  113.             pressingRightMiddleFinger = true
  114.             dist = (rightHolding.Position - rightHand.Position).Magnitude
  115.            
  116.             if rightHolding then
  117.                 remote:FireServer("Grab", rightHolding)
  118.             end
  119.         end
  120.        
  121.         if input.Position.Z <= .2 and pressingRightMiddleFinger then
  122.             if rightHolding then
  123.                 remote:FireServer("Let go", rightHolding)
  124.             end
  125.            
  126.             pressingRightMiddleFinger = false
  127.             dist = nil
  128.             rightLifting = false
  129.             rightHolding = nil
  130.         end
  131.     end
  132. end)
  133.  
  134. uis.UserCFrameChanged:Connect(function(bodyPart : Enum.UserCFrame, cframe : CFrame)
  135.     if bodyPart == Enum.UserCFrame.RightHand and not rightLifting then
  136.         FindPartToLift()
  137.     end
  138.    
  139.     if bodyPart ~= Enum.UserCFrame.RightHand then return end
  140.     if not rightHolding or not dist then return end
  141.    
  142.     local zRelative = (head.CFrame + head.CFrame.LookVector):ToObjectSpace(rightHand.CFrame).Position.Z
  143.    
  144.     if zRelative < -0.1 then
  145.         zRelative *= 5
  146.     elseif zRelative > 0.1 then
  147.         zRelative *= 2
  148.     else
  149.         zRelative = 0--deadzone so part doesnt move closer or further
  150.     end
  151.    
  152.     dist -= zRelative * 10 * delta
  153.    
  154.     remote:FireServer("Position",rightHolding, rightHand.Position + -rightHand.CFrame.RightVector * dist)
  155.     rightLifting = true
  156. end)
  157.  
  158. game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
  159.     delta = deltaTime
  160. end)
RAW Paste Data