--[[
    Creates an explosion at part's position, with specified radius. Destroys any tagged tiles within range
]]

local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")

return function(part, radius, tagName)
	if not part or not part.Parent then
		return
	end

	local position = part.Position

	local sound = part:FindFirstChild("Explode")

	if sound then
		sound.PlayOnRemove = true
	end

	local explosion = Instance.new("Explosion")
	explosion.Position = position
	explosion.BlastPressure = 0
	explosion.BlastRadius = radius
	explosion.Parent = part

	Debris:AddItem(part, 0.25)

	for _, tile in pairs(CollectionService:GetTagged(tagName)) do
		if tile and tile:IsDescendantOf(workspace) then
			local distance = (tile.Position - position).magnitude
			if distance <= radius then
				tile.Transparency = 0.5
				tile.Anchored = false
				Debris:AddItem(tile, 3)
			end
		end
	end
end
