What do you want to achieve? So, recently i have found a voxel destruction module, which works fine, however, when i regen the broken pieces they undergo the same actions from which they are broken, like unanchore, take the knockback, etc. I would be glad if someone helps me.
here is the script:
local module = {}
module.VoxelSize = 2
local destroyedParts = {}
function module:_StorePartData(part)
local partData = {
Position = part.Position,
Name = part.Name,
Size = part.Size,
CFrame = part.CFrame,
Material = part.Material,
Massless = part.Massless,
Parent = part.Parent,
Color = part.Color
}
table.insert(destroyedParts,partData)
return part
end
function module:RestoreParts()
for i,partData in ipairs(destroyedParts) do
local part = Instance.new("Part")
local newPart = part:Clone()
newPart.Name = "newPart"
newPart.Size = partData.Size
newPart.Position = partData.Position
newPart.Color = partData.Color
newPart.CFrame = partData.CFrame
newPart.Material = partData.Material
newPart.Massless = partData.Massless
newPart.Parent = partData.Parent
newPart:AddTag("regenered")
newPart.Anchored = true
end
end
function module:_ClonePart(template)
local part = template:Clone()
part.Parent = template.Parent
return part
end
function module:_GetPartsBoundInBox(cframe,size,params)
if tonumber(size) then
return workspace:GetPartBoundsInRadius(cframe.Position,size,params)
else
return workspace:GetPartBoundsInBox(cframe,size,params)
end
end
function module:dividePart(part,axis)
local a = module:_ClonePart(part)
local b = module:_ClonePart(part)
a.Size = part.Size * (-(axis/2)+Vector3.new(1,1,1))
a.CFrame = part.CFrame * CFrame.new(-part.Size * (Vector3.new(1,1,1)*axis/4))
b.Size = part.Size * (-(axis/2)+Vector3.new(1,1,1))
b.CFrame = part.CFrame * CFrame.new(part.Size * (Vector3.new(1,1,1)*axis/4))
end
function module:subdividePart(part)
if part.Size.X / 2 < module.VoxelSize and part.Size.Y / 2 < module.VoxelSize and part.Size.Z / 2 < module.VoxelSize then return false end
local axis = math.max(part.Size.X,part.Size.Y,part.Size.Z)
if axis == part.Size.X then
module:dividePart(part,Vector3.new(1,0,0))
elseif axis == part.Size.Y then
module:dividePart(part,Vector3.new(0,1,0))
else
module:dividePart(part,Vector3.new(0,0,1))
end
part:Destroy()
return true
end
function module:PartDividable(part)
if not part:IsA("Part") then return end
if part.Shape ~= Enum.PartType.Block then return end
if not part:IsDescendantOf(workspace.map) then return end
if part:IsA("Accessory") then return end
if part:HasTag("Indestructible",true) then return end
if part == workspace.map.Baseplate then return end
return true
end
function module:DestroyParts(cframe,size,destroy:boolean,velocity:Vector3,enemyRPReference:boolean,enemyRP,force,params)
local parts
local allParts
local part
repeat
parts = module:_GetPartsBoundInBox(cframe,size,params)
if #parts == 0 then break end
for i = 1, #parts do
if module:PartDividable(parts[i]) then
if part ~= parts[i] then
print("different")
else
print("same")
end
local divided = module:subdividePart(parts[i])
module:subdividePart(parts[i])
allParts = parts[i]
if not divided then
if destroy == true then
parts[i]:Destroy()
else
part = module:_StorePartData(parts[i])
parts[i].Anchored = false
parts[i].Massless = true
parts[i].CastShadow = false
parts[i]:AddTag("Indestructible")
parts[i]:AddTag("Broken")
--Create Velocity to knockback parts
local bv = Instance.new("BodyVelocity")
bv.Parent = parts[i]
bv.MaxForce = Vector3.new(10000,10000,10000)
bv.P = math.huge
if enemyRPReference == false then
bv.Velocity = velocity
else
bv.Velocity = -(enemyRP.Position - parts[i].Position).Unit * force
end
game.Debris:AddItem(bv,0.2)
-- if you want the parts to despawn after a certain amount of time use the line under this
--game:GetService("Debris"):AddItem(parts[i], 60)
end
end
else
parts[i] = nil
end
end
until #parts == 0
return allParts
end
return module
and this is the way that i call the module if it helps:
local parts
game:GetService("RunService").Heartbeat:Connect(function()
parts = voxelDestruction:DestroyParts(HItbox.CFrame,HItbox.Size,false,humanoidRootPart.CFrame.LookVector * 30,false)
end)
task.delay(3,function()
voxelDestruction:RestoreParts() -- i have to call the restoreParts func outside of the loop, obviously
end)
- What is the issue? here is a video showing the problem: