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
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)
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 singleInstance in workspace.
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
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