Part turns into debris when touched

I’m looking for ideas on how i could make a part turn into pieces when it is touched specifically client sided because i’m trying to remake the omni man train scene.

the best way i can think of doing this is by getting the size of the part, and creating small cubes to build the exact shape of the part, then unanchoring them and letting physics do the rest. demonstration:

https://gyazo.com/7cf86c7fefccf58af65f82f30e89ee5f.mp4

local deb = game:GetService("Debris")

local part = script.Parent

local broken = false

local function findGreatestDivisor(num)
	for i = 2, math.sqrt(num), 1 do
		if num % i == 0 then
			return num / i
		end
	end
	
	return num
end

part.Touched:Connect(function(p)
	local plr = game.Players:GetPlayerFromCharacter(p.Parent)
	
	local gdoX = findGreatestDivisor(part.Size.X) -- greatest divisor of X
	local gdoY = findGreatestDivisor(part.Size.Y)
	local gdoZ = findGreatestDivisor(part.Size.Z)
	
	if plr and not broken then
		broken = true
		
		part.CanCollide = false
		
		for x = gdoX, part.Size.X, gdoX do
			for y = gdoY, part.Size.Y, gdoY do
				for z = gdoZ, part.Size.Z, gdoZ do
					local piece = Instance.new("Part")
					
					piece.Size = Vector3.new(gdoX, gdoY, gdoZ)
					piece.Position = 
						part.Position 
					- Vector3.new(part.Size.X / 2, part.Size.Y / 2, part.Size.Z / 2) -- account for size of main part
					- Vector3.new(piece.Size.X / 2, piece.Size.Y / 2, piece.Size.Z / 2) -- account for size of piece
					
					piece.Position += Vector3.new(x, y, z) -- account for iterations
					
					piece.Parent = part
					deb:AddItem(piece, 2) -- remove part after some time
				end
			end
		end
		
		part.Transparency = 0.5
	end
end)

a few things to note:

  • the sizes of each debris part is calculated by getting the greatest divisor of each dimension. otherwise, the lag would be unbearable
  • this script is inside of a ServerScript, which is inside of a BasePart. you said you needed it to be local, though. the easiest way i can think of doing this is using a server-sided Touched event that fires a RemoteEvent to the client that touched it, and runs the remainder of the code.
  • using Instance.new is kind of unavoidable but also kind of bad. there’s really nothing i can do about that (to my knowledge)

hope this helps!

great! thanks for the help, i got a question though, could partcache module be better than using instance.new? (partcache is a module that creates parts already and uses it i think)

almost definitely, but i am unfamiliar with the module so you’d have to implement that yourself

alright, i was planning to implement it, just wanted to hear if it would work, thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.