Unable to Destroy a part

I am currently unable to delete a part (PrimaryPart) and roblox seems to also show it wrong in the properties window.

After i clone a room, i assign it a primary part to move it. After the room gets moved, the primary part is supposed to get deleted. Problem is: The PrimaryPart is not getting deleted. When printing the part after the Destroy line, it outputs the correct part into console (part name), but in the properties of the model, the PrimaryPart Box seems empty. When trying to change Properties like the BrickColor it also won´t work. The Part “chosenExit” is correctly destroyed.

I already tried to unassign the primary part and then deleting the part but that also did not work.

Any help would be appeciated! Here is the relevant part of the code:

local spawnedRoom = room:Clone()
spawnedRoom.PrimaryPart = entrance[math.random(1, #entrance)]
			
local exits = GetDescendantsWithTags(lastRoom, "GenNormalDoor")
local chosenExit = exits[math.random(1, #exits)]
			
spawnedRoom:SetPrimaryPartCFrame(chosenExit.CFrame * CFrame.new(0, 0, chosenExit.Size.Z) * CFrame.Angles(0, math.rad(180), 0))
spawnedRoom.Parent = spawnedRoomsFolder
spawnedRoom.PrimaryPart:Destroy()
print(spawnedRoom.PrimaryPart.Name)
chosenExit:Destroy()
2 Likes

Its probably because you didn’t remove any references to chosenExit after destroying it, so the garbage collector still considers it as a relevant variable in memory. Try:

chosenExit:Destroy()
chosenExit = nil

chosenExit is getting destroyed how it is supposed to. I am having Problems with spawnedRoom.PrimaryPart:Destroy().
trying to nil that won´t help.

Edit: i basically have 2 parts indicating the position of a doorway to connect rooms. These two parts need to be destroyed after i move a room to the given doorway. The Part of the static room (chosenExit) is getting deleted how it is supposed to. The Part of the copied and moved room (spawnedRoom.PrimaryPart) won´t be destroyed and can not be altered with properties. It does get printed as the correct part though.

What does the output print when you run the code?

The name of the part. In this case: 0a, 0b or 0c

Try setting it to nil without destroying it
spawnedRoom.PrimaryPart = nil
if that doesn’t work try using Debris
game.Debris:AddItem(spawnedRoom.PrimaryPart)

Replace function Destroy() to Remove()

remove() is very old, not supported, decapricated, and crap

1 Like

Okay, setting the PrimaryPart to nil, sets the property PrimaryPart to nil. When i after that try to delete the part (assigned it a new variable to keep track of it) it is still staying. Debris also won´t work.

I have also tried Remove() earlier even though it is deprecated and it also does not work.

when i try and access spawnedRoom.PrimaryPart from the console it is nil if i have spawnedRoom.PrimaryPart:Destroy() enabled. It returns the part if i disable the Destroy() line.

Maybe you can’t destroy the PrimaryPart of a model?
Try naming the Part something like PlacementPart and destroying Model.PlacementPart.
Or why even destroy it? Just make it Transparent CanCollide off.

5 Likes

“PrimaryPart” is a property used by roblox. You can make PrimaryPart any Part of the model. You can access the set PrimaryPart with model.PrimaryPart. The problem is that the part is not getting destroyed.
the parts names are different.

I have tried setting the PrimaryPart nil and then destroy the part which was the primary part.

I also can not edit any properties of the part in the script for some reason. When changing BrickColor through output/console it works. What?

1 Like

Just loop through the model until you find a part matching the primary part

In a second script? Otherwise it would not make much sense. I know what the part is and can easily keep track of it.

oh you do? then delete it, simple as that, also no u don’t need a second script??

it wont work

destroying it makes it nil as primarypart but the part still remains in the workspace. I also am not able to change any properties of the part in the script for some bizarre reason.

doesnt matter if i destroy it using model.PrimaryPart:Destroy() or ThePartIWantToDestroy:Destroy()

apparently there existed 2 rooms wich got cross-referenced. -.-
Whoops! Thank you all!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.