Sure! Below is a Roblox Lua script that you can place inside a Tool to allow it to cut any unanchored, can-collide part in half when swung. This script works by performing a raycast in front of the player when the tool is activated. If it detects a part, it will "cut" the part into two halves along the Y-axis at the point of impact.
Script Explanation:
-
Tool Activation:
When the tool is activated by the player (e.g., clicked), the script performs a raycast from the player's head in the direction they're facing.
-
Raycast Detection:
If the raycast hits a part, it captures the part and the exact position where it was hit.
-
Cutting the Part:
The script then calculates the local position of the hit point relative to the part and slices the part into two new parts (top and bottom halves) along the Y-axis at the hit point.
-
Creating New Parts:
It clones the original part to create the two halves, adjusts their sizes and positions accordingly, and then destroys the original part.
Limitations:
-
Axis-Aligned Cutting:
This script assumes the part is oriented along the world axes and cuts along the Y-axis. For parts that are rotated differently, additional calculations would be necessary.
-
Part Types:
The script works best with basic parts like Blocks. Cutting complex shapes or MeshParts would require more advanced handling.
-
Anchored Parts:
The script ignores anchored parts to prevent unintended behavior in your game environment.
Here is the script you can use:
-- Place this script inside the Tool
local Tool = script.Parent
local Players = game:GetService("Players")
local DebounceTime = 0.5
local debounce = false
function CutPart(part, hitPosition)
if part:IsA("BasePart") and not part.Anchored and part.CanCollide then
-- Get the local position of the hit point relative to the part
local relativeHitPos = part.CFrame:pointToObjectSpace(hitPosition)
-- Sizes and positions for the two halves
local size = part.Size
local position = part.Position
-- Calculate heights for the top and bottom parts
local topHeight = (size.Y / 2) - relativeHitPos.Y
local bottomHeight = (size.Y / 2) + relativeHitPos.Y
-- Ensure the sizes are positive
if topHeight <= 0 or bottomHeight <= 0 then
return -- Cannot cut; the hit position is outside the part
end
-- Create the top half
local topPart = part:Clone()
topPart.Size = Vector3.new(size.X, topHeight, size.Z)
local topOffsetY = relativeHitPos.Y + topHeight / 2
local topCFrame = part.CFrame * CFrame.new(0, topOffsetY, 0)
topPart.CFrame = topCFrame
topPart.Parent = part.Parent
-- Create the bottom half
local bottomPart = part:Clone()
bottomPart.Size = Vector3.new(size.X, bottomHeight, size.Z)
local bottomOffsetY = relativeHitPos.Y - bottomHeight / 2
local bottomCFrame = part.CFrame * CFrame.new(0, bottomOffsetY, 0)
bottomPart.CFrame = bottomCFrame
bottomPart.Parent = part.Parent
-- Remove the original part
part:Destroy()
end
end
function onActivated()
if debounce then return end
debounce = true
local character = Tool.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid and humanoid.Health > 0 then
local head = character:FindFirstChild("Head")
if head then
-- Define the raycast starting point and direction
local startPosition = head.Position
local direction = head.CFrame.lookVector * 5 -- Adjust the length as necessary
local params = RaycastParams.new()
params.FilterDescendantsInstances = {character}
params.FilterType = Enum.RaycastFilterType.Blacklist
-- Perform the raycast
local result = workspace:Raycast(startPosition, direction, params)
if result and result.Instance then
local hitPart = result.Instance
local hitPosition = result.Position
CutPart(hitPart, hitPosition)
end
end
end
wait(DebounceTime)
debounce = false
end
Tool.Activated:Connect(onActivated)
Instructions to Use the Script:
-
Insert a Tool into your game:
- In Roblox Studio, create a new Tool inside
StarterPack or any other appropriate location.
-
Add a Handle to the Tool:
- Ensure the Tool has a part named
Handle. This is necessary for the Tool to function correctly.
-
Insert the Script:
- Place the above script inside the Tool. You can name the script anything you like.
-
Test the Tool:
- Play your game and equip the Tool. When you click (activate the tool) while facing a part within range, it should cut the part in half.
Important Notes:
-
Safety Checks:
The script includes debounce logic to prevent rapid activation and ensures the humanoid exists and is alive before attempting to cut a part.
-
Adjustment of Raycast Length:
You can adjust the length of the raycast by changing the multiplier in direction = head.CFrame.lookVector * 5.
-
Permissions:
Ensure that the game allows script execution and that the parts you intend to cut are not protected by FilteringEnabled settings.
Enhancements:
-
Cutting Along Different Axes:
To cut along a different axis, adjust the calculations in the CutPart function accordingly.
-
Visual Effects:
You can add particle effects or sounds when cutting to enhance the visual feedback.
-
Complex Shapes:
For cutting MeshParts or more complex shapes, consider using Boolean operations or CSG (though this can be performance-intensive).
Remember to test the script thoroughly in your game to ensure it behaves as expected. If you run into any issues or need further customization, feel free to ask!