Union not being deleted

I’m working on a project because I’m back from my break, and this union I want to destroy using my custom module won’t get destroyed. I’ve tried repeating destroying it until it’s destroyed (No errors, the rest of the script works)
Script:

local module = require(game.ServerScriptService.UPStudio)
local children = game.Workspace:GetChildren()
local part1 = game.Workspace.PosParts:GetChildren()
local part2 = game.Workspace.NegParts:GetChildren()
task.wait(5)
local union = module.Union(part1, part2)
task.wait(5)
module.Separate(union) --The function that isn't working

Function in the module:

function upgradedStudio.Separate(union : table)
	local originalParts = union.OriginalParts
	local savedProperties = union.SavedProperties
	for i, v in ipairs(originalParts) do
		local propertyForCurrentPart = savedProperties[i]
		v.Transparency = propertyForCurrentPart.Transparency
		v.Name = propertyForCurrentPart.Name
		v.Anchored = propertyForCurrentPart.Anchored
		v.CanCollide = propertyForCurrentPart.CanCollide
		v.CanTouch = propertyForCurrentPart.CanTouch
		v.Parent = propertyForCurrentPart.Parent
	end
	local onion = union.Union --Don't ask why I named it onion, please
	debris:AddItem(onion, 0)
	onion:Destroy()
	repeat
		debris:AddItem(onion, 0)
		task.wait()
		print(onion)
	until not onion
end

Whilst somewhat off topic, when you destroy an object, the variable assigned to it still keeps a reference to the said object itself – you may want to check whether its parent was set to nil, or use the new Instance.Destroying (roblox.com) event.

It still exists in where I keep it in the explorer (Folder in workspace)

EDIT: Scratch that, it’s in nil, BUT there seems to be a copy union there which to my knowledge has only appeared when I call the function

EDIT 2: It’s been in nil since I created it, idk how it got there and how the copy got there

Do you mind sharing the code for this? module.Union(part1, part2)

1 Like

Here:

function upgradedStudio.Union(parts : table, negativeParts : table)
	local clonedParts = {} --table of cloned parts
	for i, v in ipairs(parts) do --loops through parts in table
		local clone = v:Clone() --clones current part
		print(clone.Parent)
		clone.Parent = v.Parent
		print(clone.Parent)
		table.insert(clonedParts, clone) --insert clone to table defined above
	end --ends for loop
	print("Loop ended")
	local firstPart = clonedParts[1] --gets the first part in the table
	print(firstPart)
	table.remove(clonedParts, 1) --removes it from the table
	local savedProperties = saveProperties(parts) --gets all the saved properties
	print("Saved properties")
	local union = firstPart:UnionAsync(clonedParts) --gets the result union
	union.Parent = firstPart.Parent
	if negativeParts ~= nil then
		union = union:SubtractAsync(negativeParts)
	end
	firstPart:Destroy()
	for i, v in ipairs(clonedParts) do
		v:Destroy()
	end
	print(union)
	print("Unioned successfully")
	for i, v in ipairs(parts) do --hides the original parts
		v.Transparency = 1
		print(v.Transparency)
		v.CanCollide = false
		v.CanTouch = false
		v.Anchored = true
		v.Parent = union
	end
	for i, v in ipairs(negativeParts) do --hides the original parts
		v.Transparency = 1
		print(v.Transparency)
		v.CanCollide = false
		v.CanTouch = false
		v.Anchored = true
		v.Parent = union
	end
	print(parts)
	local meta = {} --metatable
	meta._index = union --lorem ipsum
	local toReturn = {OriginalParts = parts, SavedProperties = savedProperties, Union = union} --table to return
	print(toReturn.Union, toReturn.Union.Parent) --Union nil (Output)
	setmetatable(toReturn, meta) --sets metatable
	return toReturn --returns toReturn
end
1 Like

This might not be relevant, but is the above programs/code all ran in Scripts or LocalScripts?

1 Like

Module is a module script (ofc) and the script is a server script in ServerScriptService

I’ve an idea, but I’m not 100% if it’ll work – what happens if you add a function to the toReturn table called DestroyUnion, where the union’s destruction would be handled in the module?

So you’re saying I gotta do something like this?

local toReturn = {OriginalParts = parts, SavedProperties = savedProperties, Union = union, DestroyUnion = function()
   toReturn.Union:Destroy()
end}

--In separate function
union.DestroyUnion()

Alright, I’ll test this out, results in a bit

1 Like

Didn’t work, also the impostor union in the folder is still there, not even the real union gets destroyed. I’ll send the place file containing only the parts which are needed in a DM (I don’t wanna leak the rest of my project to the public).

Thank you @TheeDeathCaster for replying with the solution to the PM :slight_smile:

1 Like

For those who were curious about what the solution was – I noticed that the old union wasn’t being destroyed, so I set up a new variable for the “subtracted” one, destroyed the original one and overwrote union to the new one.

1 Like