Sure! Below is a Roblox Lua script that will accomplish what you're asking for. This script will make a part react when a player comes close to it by showing a "Break" button (in English). When the player presses the E key, the part will break into fragments which will disappear after 10 seconds.
Step-by-Step Guide:
-
Insert a Part into the Workspace:
- In Roblox Studio, create a new part (or use an existing one) that you want to be breakable.
-
Add a ProximityPrompt to the Part:
- With the part selected, click the + button to add a new object.
- Add a ProximityPrompt to the part.
- Set the ActionText property of the ProximityPrompt to
"Break".
- Set the KeyboardKeyCode property to
Enum.KeyCode.E to use the E key.
-
Add the Script to the Part:
- With the part selected, click the + button to add a new object.
- Add a Script (not a LocalScript or ModuleScript) to the part.
- Rename the script to something like
"BreakScript" for clarity.
-
Copy the Following Script Code into the Script:
-- BreakScript
local part = script.Parent
local proximityPrompt = part:WaitForChild("ProximityPrompt")
-- Function to create fragments and handle breaking
local function breakPart()
-- Number of fragments to create
local fragmentCount = 10
local fragments = {}
-- Store original properties
local originalSize = part.Size
local originalCFrame = part.CFrame
local originalMaterial = part.Material
local originalColor = part.Color
-- Hide the original part
part.Transparency = 1
part.CanCollide = false
part.Anchored = true
-- Create fragments
for i = 1, fragmentCount do
local fragment = Instance.new("Part")
fragment.Size = originalSize / 2 -- Adjust fragment size as needed
-- Randomly position fragments around the original part
fragment.CFrame = originalCFrame * CFrame.new(
math.random(-2, 2),
math.random(-2, 2),
math.random(-2, 2)
)
fragment.Material = originalMaterial
fragment.Color = originalColor
fragment.Anchored = false
fragment.CanCollide = true
fragment.Parent = workspace
-- Apply random velocity to fragments
local velocity = Instance.new("BodyVelocity", fragment)
velocity.Velocity = Vector3.new(
math.random(-10, 10),
math.random(5, 15),
math.random(-10, 10)
)
velocity.MaxForce = Vector3.new(1e5, 1e5, 1e5)
-- Clean up BodyVelocity after a short time
game:GetService("Debris"):AddItem(velocity, 0.5)
-- Add fragment to the fragments table
table.insert(fragments, fragment)
end
-- Remove fragments after 10 seconds
delay(10, function()
for _, frag in ipairs(fragments) do
if frag and frag.Parent then
frag:Destroy()
end
end
-- Optionally, remove the original part completely
if part and part.Parent then
part:Destroy()
end
end)
end
-- Connect the ProximityPrompt trigger to the break function
proximityPrompt.Triggered:Connect(function(player)
breakPart()
end)
Explanation of the Script:
-
ProximityPrompt:
- Shows an interactable prompt when a player is near the part.
- The prompt says "Break" and is activated by pressing the E key.
-
breakPart Function:
- Hides the original part by setting its
Transparency to 1 and CanCollide to false.
- Creates a specified number of smaller fragment parts.
- Positions the fragments around the original part with slight random offsets.
- Applies a random velocity to each fragment to simulate explosion or breaking effect.
- Uses
Debris service to clean up BodyVelocity objects after 0.5 seconds.
- After 10 seconds, destroys all fragment parts and the original part.
Important Notes:
-
Physics Performance:
- Be cautious with the number of fragments; too many can cause lag.
- Adjust
fragmentCount as needed to balance between visual effect and performance.
-
Anchored Property:
- Fragments are unanchored to allow them to move freely under physics simulation.
-
Safety Checks:
- The script includes checks to ensure parts exist before trying to modify or destroy them.
-
Customization:
- You can adjust the size, number, and behavior of fragments by modifying the script variables.
- Customize fragment materials, colors, or add particle effects for enhanced visuals.
Testing the Script:
- Play Test:
- Click the Play button to test the game.
- Approach the part; a "Break" prompt should appear.
- Press E when the prompt is visible.
- Observe the part breaking into fragments.
- Verify that fragments disappear after 10 seconds.
Feel free to adjust and expand upon this script to better suit your game's needs!