Got it! You want the building system to only work with parts that are inside a folder named "Dragable" in the workspace. Here's the modified script with that restriction:
-- Building System Script (Dragable Folder Only)
-- 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()
-- 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
local DRAGGABLE_FOLDER_NAME = "Dragable" -- Name of the folder containing draggable parts
-- Function to check if a part can be picked up
local function canPickUp(part)
-- Only pick up parts that are in the Dragable folder
return part:IsA("BasePart") and part:IsDescendantOf(workspace:FindFirstChild(DRAGGABLE_FOLDER_NAME))
end
-- Function to check if part is inside another object
local function isPartInside(part)
local region = part:GetBoundingBox()
local minPoint = region.Position - region.Size/2
local maxPoint = region.Position + region.Size/2
-- Create a small region around the part
local regionToCheck = Region3.new(minPoint, maxPoint)
-- Get all parts in that region
local partsInRegion = workspace:ReadVoxels(regionToCheck, 4)
-- Simple collision check using raycasting
local rayOrigin = part.Position
local rayDirection = Vector3.new(0, -1, 0) * part.Size.Y
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {part}
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
return raycastResult ~= nil
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
local targetPosition = unitRay.Origin + unitRay.Direction * distance
-- 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
holding = false
-- Check if part is inside another object
if isPartInside(heldPart) then
-- Keep it anchored if inside something
heldPart.Anchored = true
heldPart.CanCollide = true
else
-- Let it fall if not inside anything
heldPart.Anchored = false
heldPart.CanCollide = 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 and canPickUp(target) then
pickUpPart(target)
end
end
end
end)
-- Keyboard input for rotation
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
rotationX = rotationX + ROTATION_INCREMENT
elseif input.KeyCode == Enum.KeyCode.T then
-- Rotate around Y axis
rotationY = rotationY + ROTATION_INCREMENT
elseif input.KeyCode == Enum.KeyCode.Y then
-- Rotate around Z axis
rotationZ = rotationZ + ROTATION_INCREMENT
elseif input.KeyCode == Enum.KeyCode.Escape then
-- Cancel pickup
if heldPart then
heldPart.CFrame = originalCFrame
heldPart.CanCollide = true
heldPart.Anchored = false
end
placePart()
end
end)
-- Handle player leaving (cleanup)
Players.PlayerRemoving:Connect(function(leavingPlayer)
if leavingPlayer == player and holding then
placePart()
end
end)
-- Visual feedback for draggable parts
local SelectionBox = Instance.new("SelectionBox")
SelectionBox.Parent = game.CoreGui
SelectionBox.Color3 = Color3.fromRGB(0, 255, 0) -- Green outline
SelectionBox.LineThickness = 0.05
mouse.Move:Connect(function()
if not holding then
local target = mouse.Target
if target and canPickUp(target) then
SelectionBox.Adornee = target
SelectionBox.Visible = true
else
SelectionBox.Visible = false
end
end
end)
print("Building system loaded! Only parts in the 'Dragable' folder can be moved.")
print("Controls: Click to pick up, R/T/Y to rotate, click to place, ESC to cancel")
Key Changes Made:
-
Folder Restriction:
- Added
DRAGGABLE_FOLDER_NAME constant
- Modified
canPickUp() to only allow parts that are descendants of the "Dragable" folder
-
Visual Feedback:
- Added a green selection box that appears when hovering over draggable parts
- The outline only appears for parts in the Dragable folder
-
Additional Check:
- Added an extra
canPickUp() check in the mouse click handler to prevent accidental pickups
How to Set Up:
- Create a folder named "Dragable" in your Workspace
- Put all parts you want to be draggable inside this folder
- Place the script in StarterGui or StarterPlayerScripts
Behavior:
- Only parts in the "Dragable" folder can be picked up
- Hovering over draggable parts shows a green outline
- All other functionality remains the same (R/T/Y rotation, placement rules, etc.)
This ensures your building system only affects the specific parts you want to be interactive!