Okay, let's modify the script so that only parts located inside a specific folder named "Dragable" in the workspace can be picked up.
We'll add a variable to store a reference to this folder and update the canPickUp function.
Here's the updated LocalScript:
-- Building System Script
-- Place this LocalScript in StarterGui or StarterPlayerScripts
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Define the folder name
local DRAGABLE_FOLDER_NAME = "Dragable"
-- Find the Dragable folder in the workspace
local dragableFolder = workspace:FindFirstChild(DRAGABLE_FOLDER_NAME)
-- Variables
local heldPart = nil
local holding = false
local connection = nil
local originalCFrame = nil
local rotationX, rotationY, rotationZ = 0, 0, 0
-- Settings
local PICKUP_DISTANCE = 50 -- Maximum distance to pick up parts
local ROTATION_INCREMENT = 15 -- Degrees to rotate per key press
-- Check if the Dragable folder exists
if not dragableFolder or not dragableFolder:IsA("Folder") then
warn("Building System: Could not find a Folder named '" .. DRAGABLE_FOLDER_NAME .. "' in Workspace. Pickup will not work.")
-- Disable the script or exit if the folder isn't found
script.Enabled = false
return
end
-- Function to check if a part can be picked up
local function canPickUp(part)
-- Check if the part is a BasePart
if not part:IsA("BasePart") then return false end
-- Check if the part's parent is the specified Dragable folder
if part.Parent ~= dragableFolder then return false end
-- Add any other conditions here (e.g., not having a specific attribute)
-- if part:GetAttribute("NoBuild") then return false end
return true
end
-- Function to check if part is inside another object (excluding the held part itself)
local function isPartInside(part)
local rayOrigin = part.Position
-- Cast a small ray downwards to see if it hits anything
local rayDirection = Vector3.new(0, -0.1, 0) -- Cast a short ray down
local rayLength = math.max(0.1, part.Size.Y / 2 + 0.1) -- Ensure ray length is at least 0.1 and goes slightly below the part
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- Exclude the held part and anything inside the Dragable folder from the check
raycastParams.FilterDescendantsInstances = {part, dragableFolder}
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
-- Also check if the part is intersecting *any* object (using GetTouchingParts)
-- This is a more robust check for being "inside"
local touchingParts = part:GetTouchingParts()
for _, touchingPart in ipairs(touchingParts) do
-- Make sure the touching part is not the player's character or something in the dragable folder
if touchingPart.Parent ~= player.Character and touchingPart.Parent ~= dragableFolder then
return true -- It's touching something outside the allowed list
end
end
-- If the downward ray hit something OR it's touching other parts, consider it "inside"
return raycastResult ~= nil or #touchingParts > 0
end
-- Function to pick up a part
local function pickUpPart(part)
if not canPickUp(part) then return end
heldPart = part
holding = true
originalCFrame = part.CFrame
rotationX, rotationY, rotationZ = 0, 0, 0
-- Make part non-collidable and anchored while holding
part.CanCollide = false
part.Anchored = true
-- Start the hold loop
connection = RunService.Heartbeat:Connect(function()
if heldPart and holding then
updateHeldPart()
end
end)
end
-- Function to update held part position
function updateHeldPart()
if not heldPart then return end
-- Cast ray from camera to mouse position
local camera = workspace.CurrentCamera
local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
-- Position the part in front of the camera
local distance = 10 -- Distance from camera
-- Add a slight vertical offset to prevent intersecting the ground immediately
local targetPosition = unitRay.Origin + unitRay.Direction * distance + Vector3.new(0, 0.5, 0)
-- Apply rotations
local rotation = CFrame.Angles(
math.rad(rotationX),
math.rad(rotationY),
math.rad(rotationZ)
)
heldPart.CFrame = CFrame.new(targetPosition) * rotation
end
-- Function to place the part
local function placePart()
if not heldPart or not holding then return end
-- Temporary unanchor and allow collision to check if it's inside anything
-- We need to do this check *before* finalizing the anchor state
heldPart.Anchored = false -- Allow physics for a moment
heldPart.CanCollide = true -- Allow collision checks
-- Wait a frame or two to allow physics engine to potentially update touching parts
RunService.Heartbeat:Wait()
RunService.Heartbeat:Wait()
local stayAnchored = isPartInside(heldPart)
if stayAnchored then
-- Keep it anchored if inside something
heldPart.Anchored = true
-- CanCollide remains true
else
-- Let it fall if not inside anything
heldPart.Anchored = false
-- CanCollide remains true
end
-- Clean up
if connection then
connection:Disconnect()
connection = nil
end
heldPart = nil
rotationX, rotationY, rotationZ = 0, 0, 0
end
-- Mouse click to pick up parts
mouse.Button1Down:Connect(function()
if holding then
placePart()
else
local target = mouse.Target
if target and target.Parent then
local distance = (target.Position - Camera.CFrame.Position).Magnitude
if distance <= PICKUP_DISTANCE then
-- Check if it's a valid part *before* attempting to pick it up
if canPickUp(target) then
pickUpPart(target)
end
end
end
end
end)
-- Keyboard input for rotation and cancel
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed or not holding or not heldPart then return end
if input.KeyCode == Enum.KeyCode.R then
-- Rotate around X axis (pitch)
rotationX = rotationX + ROTATION_INCREMENT
elseif input.KeyCode == Enum.KeyCode.T then
-- Rotate around Y axis (yaw)
rotationY = rotationY + ROTATION_INCREMENT
elseif input.KeyCode == Enum.KeyCode.Y then
-- Rotate around Z axis (roll)
rotationZ = rotationZ + ROTATION_INCREMENT
elseif input.KeyCode == Enum.KeyCode.Escape then
-- Cancel pickup
if heldPart then
-- Restore original state
heldPart.CFrame = originalCFrame
heldPart.CanCollide = true
heldPart.Anchored = false -- Unanchor it when cancelling
end
-- Clean up the held state
heldPart = nil
holding = false
rotationX, rotationY, rotationZ = 0, 0, 0
if connection then
connection:Disconnect()
connection = nil
end
end
end)
-- Handle player leaving (cleanup)
Players.PlayerRemoving:Connect(function(leavingPlayer)
if leavingPlayer == player and holding then
placePart()
end
end)
print("Building system loaded! Create a folder named 'Dragable' in Workspace and put parts inside it. Click to pick up parts, R/T/Y to rotate, click again to place, ESC to cancel")
Key Changes:
- Folder Reference:
local DRAGABLE_FOLDER_NAME = "Dragable": Defines the name of the folder.
local dragableFolder = workspace:FindFirstChild(DRAGABLE_FOLDER_NAME): Tries to find this folder in the workspace when the script starts.
- Error Check: A check is added (
if not dragableFolder or not dragableFolder:IsA("Folder") then ...) to make sure the folder exists and is actually a folder. If not, it warns the user and disables the script.
canPickUp Modification:
- The function now specifically checks if
part.Parent is equal to the dragableFolder variable.
- It also includes a check at the beginning to ensure the
dragableFolder variable is valid (which it won't be if the folder wasn't found at startup).
isPartInside Modification:
- The
isPartInside function's RaycastParams now explicitly blacklists the dragableFolder. This prevents parts within the dragable folder from counting as an "object" that the held part is inside of when checking for placement. It also adds a check for touching parts.
- Pickup Logic Update: The mouse click handler now calls
canPickUp(target) before calling pickUpPart, ensuring that only valid parts trigger the pickup process.
- Placement Check Timing: Added a couple of
RunService.Heartbeat:Wait() calls before checking isPartInside to give the physics engine a moment to register touching parts accurately after the part is temporarily unanchored.
How to Use:
- Go into Roblox Studio.
- In the Explorer window, right-click on Workspace.
- Select Insert Object.
- Choose Folder.
- Rename the newly created folder to exactly
Dragable.
- Place the parts you want to be able to pick up, rotate, and place inside this
Dragable folder.
- Put the LocalScript provided above into
StarterGui or StarterPlayerScripts.
- Run the game.
Now, the system will only interact with parts that are direct children of the "Dragable" folder in the workspace.