Create me a roblox script that brings unanchored parts to my mouse location when holding the E key and allows me to control / grab them and move them wherever my mouse is located until the E key is released, and that I can throw the part forward if I clic
Certainly! Below is a sample Roblox LocalScript that allows you to bring unanchored parts to your mouse location when holding the "E" key. You can then control and move these parts with your mouse until the "E" key is released. Additionally, if you left-click while holding the part, it will be thrown forward.
Please follow these steps to implement the script:
1. Open Roblox Studio.
2. Insert a LocalScript into `StarterPlayerScripts` or directly into a `StarterPlayer` or `StarterCharacterScripts`.
3. Paste the following code into the LocalScript.
```lua
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local holdingPart = nil
local isHolding = false
-- Helper function to get the nearest unanchored part
local function getNearestPart()
local nearest = nil
local nearestDistance = math.huge
for _, part in ipairs(workspace:GetDescendants()) do
if part:IsA("BasePart") and not part.Anchored then
local distance = (part.Position - mouse.Hit.p).magnitude
if distance < nearestDistance and distance <= 20 then -- Only consider nearby parts
nearestDistance = distance
nearest = part
end
end
end
return nearest
end
-- Function to update position of the part
local function updatePartPosition()
if holdingPart then
local targetPosition = mouse.Hit.p
holdingPart.Position = targetPosition
end
end
-- Function to throw the part
local function throwPart()
if holdingPart then
local direction = (mouse.Hit.p - holdingPart.Position).unit
holdingPart.Velocity = direction * 50 -- Adjust the throw strength as needed
holdingPart = nil
end
end
-- Key input handling
userInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.E then
local nearestPart = getNearestPart()
if nearestPart then
holdingPart = nearestPart
isHolding = true
nearestPart.Anchored = false -- Ensure the part is unanchored
end
end
if input.UserInputType == Enum.UserInputType.MouseButton1 and holdingPart then
throwPart()
end
end)
userInputService.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.E then
isHolding = false
if holdingPart then
holdingPart.Anchored = false -- Ensure it's still unanchored
holdingPart = nil
end
end
end)
-- Run a loop to keep updating the position of the part while holding E
runService.RenderStepped:Connect(function()
if isHolding then
updatePartPosition()
end
end)
```
### Explanation of the Code:
1. **Initialization**: It initializes player variables and services like `UserInputService` for detecting key presses.
2. **Getting Nearest Part**: It checks all unanchored parts in the workspace and finds the nearest one to the mouse.
3. **Updating Position**: While holding a part, its position is updated to the mouse's location.
4. **Throwing the Part**: When the left mouse button is clicked, the part is thrown in the direction from the part's position to the mouse hit position.
5. **Input Events**: The script listens for when the "E" key is pressed and released to start and stop holding the part and for mouse button clicks to throw the part.
### Important Notes:
- This code assumes that the parts you want to manipulate are in the workspace.
- Make sure unanchored parts are used for better interaction; anchored parts won't move or be thrown.
- Customize the throw strength by modifying the multiplier in the `throwPart` function.
Run this script in your game, and you should be able to interact with parts as intended!