Created
October 31, 2025 17:17
-
-
Save Quenty/03a888040942e4f29231d70fbafdcf76 to your computer and use it in GitHub Desktop.
Preserves mesh size on change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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