Create a roblox script that allows players to grab and move Part objects based on mouse location when holding the Middle Mouse Button, and that players can still interact with the object wherever it is moved based on player mouse position.
You can create a Roblox script that allows players to grab and move Part objects using the middle mouse button. Below is a LocalScript that achieves this. This should be inserted into a LocalScript within StarterPlayerScripts or StarterGui, and you need to ensure that the parts you want to manipulate are set up appropriately in the game.
Here's the script:
```lua
-- LocalScript
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local dragging = false
local draggedPart = nil
local offset = Vector3.new()
mouse.Button3Down:Connect(function()
-- Try to find a part under the mouse
local targetPart = mouse.Target
if targetPart and targetPart:IsA("Part") then
dragging = true
draggedPart = targetPart
-- Calculate the offset based on current position
offset = targetPart.Position - mouse.Hit.p
end
end)
mouse.Button3Up:Connect(function()
dragging = false
draggedPart = nil
end)
game:GetService("RunService").RenderStepped:Connect(function()
if dragging and draggedPart then
-- Move the part based on mouse position
local newPosition = mouse.Hit.p + offset
draggedPart.Position = newPosition
end
end)
-- Prevent interaction with everything else while dragging
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
-- Cancel interactions if middle mouse button (button 2) is held down
mouse.TargetFilter = draggedPart
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
mouse.TargetFilter = nil
end
end)
```
### Instructions:
1. **Place the Script**: Insert a LocalScript into either `StarterPlayerScripts` or `StarterGui`.
2. **Setup**: Ensure that the Parts you want to move are unanchored (for physics interaction) and can be interacted with.
3. **Controls**: The script uses the Middle Mouse Button (also referred to as Button 3, but you can change it to any mouse button you'd like).
### Explanation:
- The script listens for the middle mouse button being pressed (`Button3Down`). When pressed, it checks if the cursor is over a `Part` and enables dragging.
- When the button is released (`Button3Up`), the dragging stops.
- While dragging, the position of the dragged part is updated based on the mouse position adjusted by an offset to prevent jumping.
- It also prevents interaction with other objects while dragging, enhancing user experience and preventing unintended actions.
### Note:
- Adjust the functionality as needed to suit your game, especially the interaction style. You might want to consider constraints or additional functionality depending on your specific needs.


