Breaking glass in FPS games is satisfying. However, I noticed that there aren’t really any glass breaking modules on Roblox that look realistic. So, I made this module to make realistic glass breaking so it’s even more satisfying!
How to Use:
Put the module somewhere in ReplicatedStorage
You can technically put it in ServerScriptService or ServerStorage, but this module creates the fragments on the client to make the glass more responsive. If you do still choose to do this, set replicate to false in the module. When you want to break the glass, call the module:
local FractureGlass = require(game.ReplicatedStorage.FractureGlass)
FractureGlass(part, origin, force)
--[[
part: BasePart
The glass we want to fracture
origin: Vector3?
The center point of where the glass will break
Default: part.Position
force: Vector3?
The velocity that will be applied to the glass fragments
It's highly recommended that you use this parameter so the glass can fall more easily
Default: Vector3.zero
]]
Inside the module, there are many settings you can play around with. I recommend that you try to keep the number of fragments minimal to minimize lag.
YOOOOOOOOOOOO i wanted something like that for a loooong time, and now it’s here, but I have an idea, would you make it so it would work with multiple parts?
Why not just apply the function on the multiple parts? You can set the origin to be outside of the glass, so it will still work just fine. However, you might notice more triangles near the edges of the part because I have to triangulate all of the quads after clipping them. However, I don’t really think that would matter that much since the effect only lasts a couple seconds.
I think this would be a cool addition to add to the module.
In the module settings, you can customize how many triangles are generated. The way it works is that the cracks change direction after it reaches a radius. This radius is determined by the diameter variable, and it gets multiplied by the multiplier variable after the we reach the radius. The number of cracks we have is determined by the numCracks variable, and each crack is connected to the neighboring cracks at the radii. These form quads and are then clipped if they go outside the glass borders, and everything gets triangulated. I set these variables to minimize lag while still looking good since you have to keep in mind that increasing the number of triangles will increase the number of unanchored parts being added to the game which will contribute to a lot of lag if you break a lot of glass all at once.
How exactly are you planning to call this function? Are you going to have a script for each glass object or something? I was thinking you would have a single script that handles all of the glass breaking, and you could just write a loop that will call the function for each part you want to fracture at once.
@MrTumbleWede TumbleWede This is good, but how to make the part respawn after 5 seconds? I made a system, but it only makes the part respawn without the module working. Here is my script
local FractureGlass = require(game.ReplicatedStorage.FractureGlass)
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local part = script.Parent
local origin = part.Position
local force = Vector3.new(0, -9.81, 0)
FractureGlass(part, origin, force)
game.SoundService["Glass breaking"]:Play()
end
end
script.Parent.Touched:Connect(onPartTouch)
You should probably make a copy of the part before you call the FractureGlass function.
Also instead of playing the glass break sound manually, you can just set soundId in the module.
It seems like you are trying to destroy the part that the script is parented to. I recommend you don’t do that because the script will be destroyed with the part.
local FractureGlass = require(game.ReplicatedStorage.FractureGlass)
local part = script.Parent.Part -- Assuming part and script are under the same parent
local debounce = false
local function onPartTouch(otherPart)
if debounce then return end
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
debounce = true
local newPart = part:Clone()
newPart.Parent = script.Parent
-- Make the part disappear
part.Parent = game.ServerStorage
local origin = part.Position
local force = Vector3.new(0, -9.81, 0)
-- We fracture a fake part instead so we don't destroy the normal part
FractureGlass(newPart, origin, force)
task.wait(5)
-- Make the part reappear
part.Parent = script.Parent
debounce = false
end
end
part.Touched:Connect(onPartTouch)
I haven’t tested this code but hopefully it gives you a good idea.
Thanks, I made the script in ServerScriptService and applied your advice. Now it works, but for the soundId in the module, I tried and the ID was not detected, and I did not figure out how to that why i did it this way.