TheFakeFew

Shatter

Feb 10th, 2023 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.99 KB | None | 0 0
  1. function SoundEffect(parent,id,vol,pit,playonremove)
  2.     local snd = Instance.new("Sound", parent)
  3.     snd.Volume = vol
  4.     snd.SoundId = "rbxassetid://"..id
  5.     snd.Pitch = pit
  6.     snd.PlayOnRemove = playonremove or false
  7.     if(playonremove)then
  8.         snd:Destroy()
  9.     else
  10.         snd:Play()
  11.     end
  12.     game:GetService("Debris"):AddItem(snd, snd.TimeLength/snd.Pitch)
  13. end
  14.  
  15. function CleanObject(obj,keep)
  16.     local function clean(v)
  17.         if v:IsA("DataModelMesh") and not table.find(keep,"SpecialMesh") then
  18.             v:Destroy()
  19.         elseif v:IsA("MeshPart") and not table.find(keep,"MeshPart") then
  20.             local a = Instance.new("Part", v.Parent)
  21.             a.Name = v.Name
  22.             a.Size = v.Size
  23.             a.CFrame = v.CFrame
  24.             a.Material = v.Material
  25.             a.Color = v.Color
  26.             a.Transparency = v.Transparency
  27.             a.Anchored = v.Anchored
  28.             a.CanCollide = v.CanCollide
  29.             a.CanQuery = v.CanQuery
  30.             a.Parent = v.Parent
  31.             pcall(game.Destroy,v)
  32.         elseif v:IsA("Sound") and not table.find(keep,"Sound") then
  33.             v.PlayOnRemove = false
  34.             v:Destroy()
  35.         elseif v:IsA("Decal") and not table.find(keep,"Decal") then
  36.             v:Destroy()
  37.         elseif v:IsA("JointInstance") and not table.find(keep,"JointInstance") then
  38.             v:Destroy()
  39.         elseif v:IsA("Script") and not table.find(keep,"Script") then
  40.             v.Disabled = true
  41.             v:Destroy()
  42.         elseif v:IsA("LocalScript") and not table.find(keep,"LocalScript") then
  43.             v.Disabled = true
  44.             v:Destroy()
  45.         elseif v:IsA("ModuleScript") and not table.find(keep,"ModuleScript") then
  46.             v:Destroy()
  47.         elseif v:IsA("Attachment") and not table.find(keep,"Attachment") then
  48.             v:Destroy()
  49.         elseif v:IsA("ParticleEmitter") and not table.find(keep,"ParticleEmitter") then
  50.             v:Destroy()
  51.         elseif v:IsA("PointLight") and not table.find(keep,"PointLight") then
  52.             v:Destroy()
  53.         end
  54.     end
  55.     clean(obj)
  56.     for i,v in next, obj:GetDescendants() do
  57.         clean(v)
  58.     end
  59. end
  60.  
  61. function Shatter(p) --thx WomanMalder UwU
  62.     local function isdestroyed(inst)
  63.         if (inst.Parent ~= nil) then return (false) end
  64.         local _, b = pcall(function()
  65.             inst.Parent = inst
  66.         end)
  67.         if(b:match('locked'))then
  68.             return (true)
  69.         else
  70.             return (false)
  71.         end
  72.     end;
  73.  
  74.     local function Subtract(Part:BasePart,Negation:{Instance}|Instance,CollisionFidelity:Enum.CollisionFidelity|nil)
  75.         if(CollisionFidelity==nil)then CollisionFidelity = 'Hull' end
  76.         if(typeof(Negation)=='table')then
  77.             for o, p in next, Negation do
  78.                 if (p:IsDescendantOf(workspace)) then
  79.                 else
  80.                     return
  81.                 end
  82.             end
  83.             return(Part:SubtractAsync(Negation, CollisionFidelity));
  84.         else
  85.             if(Part:IsDescendantOf(workspace))then
  86.                 return(Part:SubtractAsync({Negation}, CollisionFidelity));
  87.             end
  88.         end
  89.     end;
  90.  
  91.     local function Fragment(Part, Count, Depth)
  92.         local fragCount = 0;
  93.         local Fragments = {};
  94.         local partSize = Part.Size;
  95.         local partCF = Part.CFrame;
  96.  
  97.         if(Part:IsDescendantOf(workspace) and Count >= 0)then
  98.             local c1 = Instance.new('Part')
  99.             c1.Size = partSize*4
  100.             c1.CFrame = partCF * CFrame.Angles(math.rad(math.random(-360,360)),math.rad(math.random(-360,360)),math.rad(math.random(-360,360))) * CFrame.new(0, -partSize.Y * 2, 0)
  101.             local c2 = c1:Clone()
  102.             c2.CFrame = partCF * CFrame.Angles(math.rad(math.random(-360,360)),math.rad(math.random(-360,360)),math.rad(math.random(-360,360))) * CFrame.new(0, partSize.Y * 2, 0)
  103.             local p1, p2
  104.             pcall(function()
  105.                 p1 = Subtract(Part, c1)
  106.                 p2 = Subtract(Part, c2)
  107.             end)
  108.             if (Depth ~= 0) then
  109.                 Part.Parent = nil
  110.             end
  111.             if(p1 and p2 and fragCount <= 65) then
  112.                 p1.CFrame = partCF * partCF:ToObjectSpace(p1.CFrame)
  113.                 p2.CFrame = partCF * partCF:ToObjectSpace(p2.CFrame)
  114.                 p1.Parent = Part.Parent
  115.                 p2.Parent = Part.Parent
  116.                 local f1 = Fragment(p1, Count-1, Depth+1)
  117.                 local f2 = Fragment(p2, Count-1, Depth+1)
  118.                 table.insert(Fragments, p1)
  119.                 table.insert(Fragments, p2)
  120.                 fragCount = fragCount + 2
  121.                 p1.AncestryChanged:Connect(function(inst, parent)
  122.                     if (parent == nil and isdestroyed(inst)) then
  123.                         fragCount = fragCount - 1
  124.                     end
  125.                 end)
  126.                 p2.AncestryChanged:Connect(function(inst, parent)
  127.                     if (parent == nil and isdestroyed(inst)) then
  128.                         fragCount = fragCount - 1
  129.                     end
  130.                 end)
  131.                 for i, v in next, f1 do
  132.                     table.insert(Fragments, v)
  133.                 end
  134.                 for i, v in next, f2 do
  135.                     table.insert(Fragments, v)
  136.                 end
  137.             end
  138.         end
  139.         for i, v in next, Fragments do
  140.             v.Parent = nil
  141.         end
  142.         if(#Fragments == 0) then
  143.             Fragments = {Part:Clone()}
  144.         end
  145.         return (Fragments)
  146.     end
  147.  
  148.     local function getbiggestaxis(vector)
  149.         local biggest = 0
  150.         if(vector.X>biggest)then
  151.             biggest = vector.X
  152.         end
  153.         if(vector.Y>biggest)then
  154.             biggest = vector.Y
  155.         end
  156.         if(vector.Z>biggest)then
  157.             biggest = vector.Z
  158.         end
  159.         return biggest
  160.     end
  161.  
  162.     local function shatterify(b)
  163.         if(b:IsA("BasePart"))then
  164.             b.Anchored = true
  165.             local fragments = Fragment(b, -1, 5)
  166.             for i,v in next, fragments do
  167.                 coroutine.wrap(function()
  168.                     v.Anchored = true
  169.                     v.Parent = workspace
  170.                     v.Material = Enum.Material.Glass
  171.                     local biggest = 1+(getbiggestaxis(v.Size)/2)
  172.                     local pos = v.Position + (Vector3.new(math.random(-3,3),math.random(2,5),math.random(-3,3))*Vector3.new(biggest,biggest,biggest))
  173.                     local mag = (v.Position - pos).Magnitude
  174.                     game:GetService("TweenService"):Create(v,TweenInfo.new((mag/2)/biggest, Enum.EasingStyle.Back),{
  175.                         Position = pos,
  176.                         Size = v.Size/2,
  177.                         Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
  178.                     }):Play()
  179.                     task.wait((mag/3)/biggest)
  180.                     local fragments2 = Fragment(v, 1, 5)
  181.                     v:Destroy()
  182.                     for i,a in next, fragments2 do
  183.                         coroutine.wrap(function()
  184.                             a.Anchored = true
  185.                             a.Parent = workspace
  186.                             a.Material = Enum.Material.Glass
  187.                             SoundEffect(a,7140152893,1,math.random(70,120)/100,true)
  188.                             biggest = 1+(getbiggestaxis(a.Size)/2)
  189.                             local pos = a.Position + (Vector3.new(math.random(-5,5),math.random(-5,5),math.random(-5,5))*Vector3.new(biggest,biggest,biggest))
  190.                             local mag = (a.Position - pos).Magnitude
  191.                             game:GetService("TweenService"):Create(a,TweenInfo.new(mag/biggest),{
  192.                                 Position = pos,
  193.                                 Size = Vector3.new(),
  194.                                 Transparency = 1,
  195.                                 Orientation = Vector3.new(math.random(-360,360),math.random(-360,360),math.random(-360,360))
  196.                             }):Play()
  197.                             task.wait((mag)/biggest)
  198.                             a:Destroy()
  199.                         end)()
  200.                     end
  201.                 end)()
  202.             end
  203.             b:Destroy()
  204.         end
  205.     end
  206.     if(p:IsA("Model") or p:IsA("Folder") or p:IsA("WorldModel"))then
  207.         SoundEffect(p:FindFirstChildWhichIsA("Part",true),9125402735,10,math.random(70,120)/100,true)
  208.         CleanObject(p,{})
  209.         for i,b in next, p:GetDescendants() do
  210.             if(b:IsA("BasePart"))then
  211.                 coroutine.wrap(function()
  212.                     shatterify(b)
  213.                 end)()
  214.             end
  215.         end
  216.         p:Destroy()
  217.     elseif(p:IsA("BasePart"))then
  218.         SoundEffect(p,9125402735,10,math.random(70,120)/100,true)
  219.         CleanObject(p,{})
  220.         coroutine.wrap(function()
  221.             shatterify(p)
  222.         end)()
  223.         for i,v in next, p:GetDescendants() do
  224.             if(v:IsA("BasePart"))then
  225.                 coroutine.wrap(function()
  226.                     shatterify(v)
  227.                 end)()
  228.             end
  229.         end
  230.         p:Destroy()
  231.     end
  232. end
  233.  
  234. function getPlayersFromString(plr,str)
  235.     local returntbl = {}
  236.     if str == nil then
  237.         table.insert(returntbl,plr)
  238.         return returntbl
  239.     end
  240.     local comma = string.split(str,",")
  241.     for i,v in next, comma do
  242.         if getPlayer(v) and not table.find(returntbl,getPlayer(v)) then
  243.             table.insert(returntbl,getPlayer(v))
  244.         end
  245.         if string.lower(v) == "me" or string.lower(v) == "" then
  246.             table.insert(returntbl,plr)
  247.         elseif string.lower(v) == "others" then
  248.             for i,v in next, game:GetService('Players'):GetPlayers() do
  249.                 if v ~= plr then
  250.                     table.insert(returntbl,v)
  251.                 end
  252.             end
  253.         elseif string.lower(v) == "all" then
  254.             for i,v in next, game:GetService('Players'):GetPlayers() do
  255.                 table.insert(returntbl,v)
  256.             end
  257.         end
  258.     end
  259.     return returntbl
  260. end
  261.  
  262. function getPlayer(str)
  263.     if str == nil or str == "" then
  264.         return nil
  265.     end
  266.     str = string.lower(str)
  267.     for i,v in next, game:GetService('Players'):GetPlayers() do
  268.         if string.sub(string.lower(v.Name),1,string.len(str)) == str then
  269.             return v
  270.         end
  271.     end
  272. end
  273.  
  274. local touchconnections = {}
  275. local ontouch = false
  276.  
  277. owner.Chatted:Connect(function(m)
  278.     local msg = m
  279.     if(msg:sub(1,3) == "/e ")then
  280.         msg = m:sub(4)
  281.     end
  282.     local split = string.split(string.lower(msg),"!")
  283.     if(split and split[1] == "shatter")then
  284.         local plrs = getPlayersFromString(owner, split[2])
  285.         for i,v in next, plrs do
  286.             if(v.Character)then
  287.                 Shatter(v.Character)
  288.             end
  289.         end
  290.     elseif(split and split[1] == "ontouch")then
  291.         for i,v in next, touchconnections do
  292.             pcall(function() v:Disconnect() end)
  293.         end
  294.         ontouch = not ontouch
  295.         if(ontouch)then
  296.             for i,v in next, owner.Character:GetDescendants() do
  297.                 if(v:IsA("BasePart")) then
  298.                     table.insert(touchconnections, v.Touched:Connect(function(part)
  299.                         local m = part:FindFirstAncestorOfClass("Model")
  300.                         if(m and m:FindFirstChildOfClass("Humanoid"))then
  301.                             Shatter(m)
  302.                         end
  303.                     end))
  304.                 end
  305.             end
  306.         end
  307.     end
  308. end)
Advertisement
Add Comment
Please, Sign In to add comment