How can i optimize this? (over 4000 parts)

Basically, i’m making it so that every destructable part can be collected as a currency but there is an issue with that. the map is huge and theres over 4k destructable parts. and i don’t want to set up an anchor property listener to all of them it will lag the game
this is how i’m getting all the objects

game.Loaded:Wait()
for _, object in workspace:GetDescendants() do	
	local SnapObject = object:FindFirstChildOfClass("Snap")
	if not SnapObject then continue end
end

That’s what I would do. Anyways, that only runs once when the server is being setup. Some games just have longer loading.

Wouldn’t you be listening for .Touched?

1 Like

I’m not totally sure how to do this but, I’m sure what you’re looking for is CollectionService.
I know that workspace:GetDescendants() will be problematic.

A bit of a guess here but, it’s something like this.

local CollectionService = game:GetService("CollectionService")

for _, object in CollectionService:GetTagged("Destructible") do
	local SnapObject = object:FindFirstChildOfClass("Snap")
	if SnapObject then
		-- Handle object
	end
end

CollectionService:GetInstanceAddedSignal("Destructible"):Connect(function(object)
	local SnapObject = object:FindFirstChildOfClass("Snap")
	if SnapObject then
		-- Handle new object
	end
end)
3 Likes

No because i want to know when a part has been destroyed, when that happens the anchor get’s set back to false

When a part is destroyed, use part.Destroying

1 Like

That just seems like unnecessary, i would have to loop through the workspace and then tag the object and then loop through the collectionservice tags.

They aren’t being destroyed tho, The game has destruction physics with snaps. So they just get unanchored after getting “Destroyed”

CollectionService can do that well. May be worth the time. Good luck!

1 Like

Use

yourPart:FindFirstChildWhichIsA("Snap").Destroying

I believe snaps are deprecated anyway.

1 Like

@2112Jay meant you should tag the parts before-hand

1 Like

Yeah no that is not happening. As i mentioned i have over 4000 destructive parts, the best option i have is to loop through all of workspace

Why make the post, and then decline every solution they offer you, then just say like

Nope i gotta do this, bye.

if you already knew what to do, why make the post!

:confused:

2 Likes

I’m looking for suggestions, i appreciate any help.

1 Like

Assuming that all of your workspace’s parts are not destructible then you can do:

local Game,Workspace = game,workspace
Game.Loaded:Wait()

local DestructibleParts = {}

for _, object in Workspace:GetDescendants() do
	local SnapObject = object:FindFirstChildOfClass("Snap")
	if not SnapObject then continue end

	table.insert(DestructibleParts,object)
end

while #DestructibleParts > 0 do
	task.wait(1)

	for index,object in DestructibleParts do
		if object.Anchored then continue end
		table.remove(DestructibleParts,index)

		-- Handle Unanchored state here.
	end
end

In the code, we first declare all destructible parts to a table, so, we won’t be using CollectionService, next, we use a while loop that will loop through the table we just created and if it finds any part which is not anchored then it will swiftly remove the part from the table so we won’t have to loop through the same part again.

@2112Jay is correct. CollectionService is by and far the best solution for this problem; its whole purpose is to keep track of specific Instances as declared by the developer. Tag all SnapObjects and use CollectionService to retrieve them instead of iterating through every single Instance in workspace.

1 Like

Should i like tag all the parts initially from the server?

2112Jay post is the solution
as you would loop through specific instances rather than the whole workspace

also, if you don’t want to tag all the instances manually then you can run this through the command line

for _, object in workspace:GetDescendants() do	
	local SnapObject = object:FindFirstChildOfClass("Snap")
	if not SnapObject then continue end
	SnapObject:AddTag("SnapObject")
end

Disable unnecesary collisions and reduce fidelty

If you have problem to detect if part was destroyed, then instead of part-based collection, use player based one, have hitbox attached to player and detect if part that was hit was destructable

You can tag parts that were “destroyed” with collection service, and then check if part that touched player’s hitbox had this tag, based off that you don’t need 4k connections, rather 20 or so