Feedback of my upcoming plugin code and how do I make it work?

Both an #help-and-feedback:code-review and #help-and-feedback:scripting-support topic

How do you think of this code, and how do I get it to work

plugineee.rbxm (45.8 KB)

local Selection = game:GetService("Selection")
local HttpService = game:GetService("HttpService")
local MaterialService = game:GetService("MaterialService")
local InsertService = game:GetService("InsertService")
local ChangeHistoryService = game:GetService("ChangeHistoryService")

local modules = script:WaitForChild("Modules")

local Notify = require(modules:WaitForChild("BannerNotificationModule"))
local Fusion = require(modules:WaitForChild("Fusion"))

local new = Fusion.New
local children = Fusion.Children

local random = Random.new()

local toolbar = plugin:CreateToolbar("")
local button = toolbar:CreateButton("InstanConvert", "", "rbxassetid://13537076676")

local menu = plugin:CreatePluginMenu(random:NextNumber(1, 10))
local MaterialMenu = plugin:CreatePluginMenu(random:NextNumber(1, 10), "Material")
local MeshMenu = plugin:CreatePluginMenu(random:NextNumber(1, 10), "Mesh")

MaterialMenu:AddNewAction("ConvertMaterial", "Convert SurfaceAppearance", "rbxassetid://13538205485")
MaterialMenu:AddNewAction("UndoConvertMaterial", "Convert Material", "rbxassetid://13056246128")

MeshMenu:AddNewAction("ConvertMesh", "Convert MeshPart", "rbxassetid://13538205485")
MeshMenu:AddNewAction("UndoConvertMesh", "Convert SpecialMesh", "rbxassetid://13056246128")

menu:AddMenu(MaterialMenu)
menu:AddSeparator()
menu:AddMenu(MeshMenu)

local warning = "rbxassetid://11419713314" -- the Warning Icon
local success = "rbxassetid://11419719540" -- the Success Icon
local prefix = "[InstanConvert]:"

type MaterialProperties = {
	part: BasePart,
	sa: SurfaceAppearance?,
	image: Decal | Texture?,
}

type configs = Notify.configs

local err: configs = {
	BackgroundColor = Color3.new(0.75),
	ContentColor = Color3.new(0.75),
}

local function RemoveParent(item: Instance)
	task.delay(0.1, function()
		item.Parent = nil
	end)
end

local function ConvertMaterial(properties: MaterialProperties)
	local instance = properties.part
	local image = properties.image
	local surfaceAppearance = properties.sa
	if surfaceAppearance then
		local material = new("MaterialVariant")({
			Name = if instance.Name ~= instance.ClassName
				then instance.Name
				else HttpService:GenerateGUID(false):gsub("-", ""),
			ColorMap = surfaceAppearance.ColorMap,
			NormalMap = surfaceAppearance.NormalMap,
			MetalnessMap = surfaceAppearance.MetalnessMap,
			RoughnessMap = surfaceAppearance.RoughnessMap,
			BaseMaterial = instance.Material,
			Parent = MaterialService,
		})

		Selection:Set({ material })

		RemoveParent(surfaceAppearance)
		instance.MaterialVariant = material.Name

		ChangeHistoryService:SetWaypoint("MaterialConveted")
		Notify(prefix, "Successfuly converted a SurfaceAppearance to a MaterialVariant", success, 10)
	end

	if image then
		if typeof(image) == "Decal" then
			local material = new("MaterialVariant")({
				Name = if instance.Name ~= instance.ClassName
					then instance.Name
					else HttpService:GenerateGUID(false):gsub("-", ""),
				ColorMap = image.Texture,
				BaseMaterial = instance.Material,
				Parent = MaterialService,
			})

			Selection:Set({ material })

			RemoveParent(image)
			instance.MaterialVariant = material.Name

			ChangeHistoryService:SetWaypoint("MaterialConveted")
			Notify(prefix, "Successfuly converted a Decal to a MaterialVariant", success, 10)
		elseif typeof(image) == "Texture" then
			local material = new("MaterialVariant")({
				Name = if instance.Name ~= instance.ClassName
					then instance.Name
					else HttpService:GenerateGUID(false):gsub("-", ""),
				ColorMap = image.Texture,
				StudsPerTile = (image.StudsPerTileU + image.StudsPerTileV) / 2,
				BaseMaterial = instance.Material,
				Parent = MaterialService,
			})

			Selection:Set({ material })

			RemoveParent(image)
			instance.MaterialVariant = material.Name

			ChangeHistoryService:SetWaypoint("MaterialConveted")
			Notify(prefix, "Successfuly converted a Texture to a MaterialVariant", success, 10)
		end
	end
end

button.Click:Connect(function()
	local selected = menu:ShowAsync()

	if selected then
		if selected.ActionId:find("ConvertMaterial") then
			for _, value in Selection:Get() do
				if value:IsA("SurfaceAppearance") then
					if value.Parent:IsA("MeshPart") then
						ConvertMaterial({ part = value.Parent, sa = value })
					end
				elseif value:IsA("MeshPart") then
					if value:FindFirstChildOfClass("SurfaceAppearance") then
						local SurfaceAppearance = value:FindFirstChildOfClass("SurfaceAppearance")
						ConvertMaterial({ part = value, sa = SurfaceAppearance })
					else
						Notify(
							prefix,
							`Mesh must have a SurfaceAppearance, got {table.concat(value:GetChildren(), ", ")}`,
							warning,
							10,
							err
						)
					end
				elseif value:IsA("Decal") then
					ConvertMaterial({ part = value.Parent, image = value })
				elseif value:IsA("Texture") then
					ConvertMaterial({ part = value.Parent, image = value })
				elseif value:IsA("BasePart") then
					if value:FindFirstChildOfClass("Decal") then
						local decal = value:FindFirstChildOfClass("Decal")
						ConvertMaterial({ part = value, image = decal })
					elseif value:FindFirstChildOfClass("Texture") then
						local texture = value:FindFirstChildOfClass("Texture")
						ConvertMaterial({ part = value, image = texture })
					end
				else
					Notify(
						prefix,
						`Selected Item must be a SurfaceAppearance, MeshPart, Decal, Texture, or BasePart, got {value.ClassName}`,
						warning,
						10,
						err
					)
				end
			end
		elseif selected.ActionId:find("UndoConvertMaterial") then
			for _, value in Selection:Get() do
				if value:IsA("MaterialVariant") then
					local MeshPart = new("MeshPart")({
						Parent = workspace,

						[children] = {
							new("SurfaceAppearance")({
								Name = value.Name,
								ColorMap = value.ColorMap,
								NormalMap = value.NormalMap,
								MetalnessMap = value.MetalnessMap,
								RoughnessMap = value.RoughnessMap,
							}),
						},
					})

					Selection:Set({ MeshPart })

					task.delay(0.1, RemoveParent, value)

					ChangeHistoryService:SetWaypoint("MaterialConveted")
					Notify(prefix, "Successfuly converted a MaterialVariant to a SurfaceAppearance", success, 10)
				end
			end
		elseif selected.ActionId:find("ConvertMesh") then
			for _, value in Selection:Get() do
				if value:IsA("MeshPart") then
					local SpecialMesh = new("Part")({
						Parent = workspace,

						[children] = {
							new("SpecialMesh")({
								Name = value.Name,
								MeshId = value.MeshId,
								TextureId = value.TextureID,
							}),
						},
					})

					Selection:Set({ SpecialMesh })

					RemoveParent(value)

					ChangeHistoryService:SetWaypoint("MeshConverted")
					Notify(prefix, "Successfuly converted a MeshPart to a SpecialMesh", success, 10)
				else
					Notify(prefix, `Selected Item must be a MeshPart, got {value.ClassName}`, warning, 10, err)
				end
			end
		elseif selected.ActionId:find("UndoConvertMesh") then
			for _, value in Selection:Get() do
				if value:IsA("SpecialMesh") then
					local MeshPart = InsertService:CreateMeshPartAsync(
						value.MeshId,
						Enum.CollisionFidelity.Default,
						Enum.RenderFidelity.Automatic
					)
					MeshPart.TextureID = value.TextureId
					MeshPart.Parent = workspace

					Selection:Set({ MeshPart })

					RemoveParent(value.Parent)

					ChangeHistoryService:SetWaypoint("MeshConverted")
					Notify(prefix, "Successfuly converted a SpecialMesh to a MeshPart", success, 10)
					if value:IsA("BasePart") then
						if value:FindFirstChildWhichIsA("SpecialMesh") then
							local Mesh = value:FindFirstAncestorWhichIsA("SpecialMesh")
							local MeshPart = InsertService:CreateMeshPartAsync(
								Mesh.MeshId,
								Enum.CollisionFidelity.Default,
								Enum.RenderFidelity.Automatic
							)
							MeshPart.TextureID = Mesh.TextureId
							MeshPart.Parent = workspace

							Selection:Set({ MeshPart })

							RemoveParent(value)

							ChangeHistoryService:SetWaypoint("MeshConverted")
							Notify(prefix, "Successfuly converted a SpecialMesh to a MeshPart", success, 10)
						end
					end
				else
					Notify(
						prefix,
						`Selected Item must be a SpecialMesh or BasePart, got {value.ClassName}`,
						warning,
						10,
						err
					)
				end
			end
		end
	end
end)

How do you rate the code
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

0 voters

2 Likes