Skip to content

Instantly share code, notes, and snippets.

@Quenty
Created October 31, 2025 17:17
Show Gist options
  • Select an option

  • Save Quenty/03a888040942e4f29231d70fbafdcf76 to your computer and use it in GitHub Desktop.

Select an option

Save Quenty/03a888040942e4f29231d70fbafdcf76 to your computer and use it in GitHub Desktop.
Preserves mesh size on change
-- Run this in the command line in Studio to preserve mesh size when bulk replacing meshes.
local Selection = game:GetService("Selection")
-- Table to store per-MeshPart tracking data
local trackedParts: {[MeshPart]: {
originalSize: Vector3,
lastMeshId: string,
connections: {RBXScriptConnection}
}} = {}
-- Clean up all connections for a given MeshPart
local function untrackPart(part: MeshPart)
local data = trackedParts[part]
if not data then return end
for _, conn in ipairs(data.connections) do
conn:Disconnect()
end
trackedParts[part] = nil
end
-- Start tracking a MeshPart
local function trackPart(part: MeshPart)
if trackedParts[part] then return end
local data = {
originalSize = part.Size,
lastMeshId = part.MeshId,
connections = {}
}
trackedParts[part] = data
-- MeshId change listener
table.insert(data.connections, part:GetPropertyChangedSignal("MeshId"):Connect(function()
if part.MeshId ~= data.lastMeshId then
part.Size = data.originalSize
data.lastMeshId = part.MeshId
print(("[Plugin] Reset size for %s"):format(part:GetFullName()))
end
end))
-- Clean up when part is removed
table.insert(data.connections, part.AncestryChanged:Connect(function(_, parent)
if parent == nil then
untrackPart(part)
end
end))
end
-- Refresh which parts are being tracked whenever selection changes
Selection.SelectionChanged:Connect(function()
-- Untrack parts no longer selected
for part in pairs(trackedParts) do
if not table.find(Selection:Get(), part) then
untrackPart(part)
end
end
-- Track all MeshParts currently selected
for _, obj in ipairs(Selection:Get()) do
if obj:IsA("MeshPart") then
trackPart(obj)
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment