Collection Service or Tables

Lately I’ve been using tables to separate things like teams and stuff, I was wondering if it was more efficent to just use collection service or keep using tables.

  • Keep using tables
  • Start using Collection Service
  • Use both

0 voters

If you think I should start using Collection Service may I get some explanation if you have the time to? Just want to be more informed on what’s more efficent and what isn’t.

it depends, I use loops if I want to use a .touched event for a group of parts that need to have the same behavior. But if it is impractical to use loops such as changing a lot of parts behaviour during runtime then use collection service.

2 Likes

One can be more efficient than the latter, it depends on the situation.

Simple data such as numbers, tables.
One category, tables.
Script accessibility, CollectionService.
Specific assets that is hard to find and is constantly added to the game, CollectionService.

There are much more, just reflect on which one would be easier on you.

1 Like

Ah I see, one of the few situations I would use CollectionService would be the script accessibility one you mentioned, since the fourth one I can pretty much do with table.insert(), thanks!

1 Like

I would use tables, I think they would be more efficient in my opinion. Depending on if collectionservice uses hashtables or arrays internally. Plus they’re more fliexable cause you can have anything passed by reference in a custom one such as tables not just instances. And it’s pretty easy to impelement to, here would be a CollectionEngine, I just wrote just now

-- this collectionENGINE works for any thing which is passed by reference

-- for eg: TABLES, INSTANCES, USERDATA, FUNCTIONS, THREADS
local TagEngine = {}
TagEngine.TagsOfItemTable = {} -- basically one table has item and its tags
TagEngine.ItemsOfTagTable = {} -- other has TAGS and it's items --  both tables made to not having looping

TagEngine.TotalTags = 0
TagEngine.TotalItems = 0

TagEngine.ItemAdded = function(Item, Tag) end
TagEngine.ItemRemoved = function(Item, Tag) end

function TagEngine:AddTag(Item, Tag)
	if TagEngine.ItemsOfTagTable[Tag] == nil then TagEngine.ItemsOfTagTable[Tag] = {} TagEngine.TotalTags +=1 end
	if TagEngine.TagsOfItemTable[Item] == nil then TagEngine.TagsOfItemTable[Item] = {} TagEngine.TotalItems +=1 end
	
	TagEngine.TagsOfItemTable[Item][Tag] = Tag
	TagEngine.ItemsOfTagTable[Tag][Item] = Item
	TagEngine.ItemAdded(Item, Tag)
end

function TagEngine:RemoveTag(Item, Tag)
	TagEngine.TagsOfItemTable[Item][Tag] = nil
	TagEngine.ItemsOfTagTable[Tag][Item] = nil
	
	TagEngine.ItemRemoved(Item, Tag)
end

function TagEngine:GetTags(Item)
	local TagTable = {}
	local Incrementor = 0
	for _,v in pairs(TagEngine.TagsOfItemTable) do
		Incrementor+=1
		TagTable[Incrementor] = v
	end
	
	return TagTable
end

function TagEngine:HasTag(Item, Tag)
	return TagEngine.TagsOfItemTable[Item][Tag] ~= nil
end

function TagEngine:GetItemsOfTag(Tag)
	local ItemTable = {}
	local Incrementor = 0
	
	for _,Item in pairs(TagEngine.ItemsOfTagTable[Tag]) do
		Incrementor+=1 
		ItemTable[Incrementor] = Item
	end
	
	return ItemTable
end

return TagEngine

— EXAMPLE USAGE
local CollectionEngine = require(script.Parent.CollectionEngine)

local m = {'as', 'as'}

local z = workspace.Baseplate

CollectionEngine.ItemAdded = function(Item, Tag) -- yah callbacks are neater then events
	if Tag == 'deadTables' then
		table.remove(Item,1)
	elseif Tag == 'yo' then
		if typeof(Item) == 'Instance' then
			Item.Transparency = 1
		end
	end	
end

local MyTable = {'helo', 'lol'}

CollectionEngine:AddTag(MyTable, 'deadTables')

CollectionEngine:AddTag(workspace.Baseplate, 'yo')
print(CollectionEngine:HasTag(workspace.Baseplate, 'yo'))

for i,v in pairs(CollectionEngine:GetItemsOfTag('yo')) do
	print (v)
end

CollectionEngine:RemoveTag(workspace.Baseplate, 'yo')

print(CollectionEngine:HasTag(workspace.Baseplate, 'yo'))

for i,v in pairs(CollectionEngine:GetItemsOfTag('yo')) do
	print (v)
end

print '-------------------------------'
print 'These are the items in MyTable'
for i,v in pairs(MyTable) do
	print(v)
end
print '---------------------------------'

print('total amount of items in tag engine is '.. CollectionEngine.TotalItems)

Oh yah and:


https://gyazo.com/c6153d2a4e68a81e3c7cd22f8bc8219a

So yah I think it’s more flexible, and efficient to make a custom collectionservice using tables (Last one is a maybe not sure if it’s more efficient). Since the above one I showed you has .ItemRemoved, but in roblox’s collectionservice it’s depreicated.

This alllows object pooling systems with custom classes to.

Hard thing to do would be replication, collection service tagging replicates to clients instantly.

3 Likes

Nooo don’t use table.insert() and performant a linear search if that’s what your doing.

Use hashtables I have a post above which shows you how to do it.

1 Like

Well, I use Collection Service and actively tag new objects instanced into my game and when things like for loops are running my workspace.ChildAdded Event to tag new parts doesn’t fire. So I’m confused on what to do cuz they both have for loops. Also, I tried 2 scripts but it didn’t work since tags don’t replicate between scripts…

Can u please explain what hash tables are?

Hash Tables are lua tables. In lua we can consider an array a hashtable to. Hashtabes can have any indexes such as numbers, objects, tables, userdata, threads, and even functions.

Hashtables allow us to store multiple pieces of data in one variable using a concept of indexes and values. When you index a hashtable you get the corresponding value back:

local MyHashTable = {

Lol = “lo”;
Ya = “hee”
}

So now to the index we put the name of the index followed by a dot.

So print(MyHashTable.Ya) would print hee, since the index Ya corresponds to hee as we see above.

So are they basically are arrays? or are tables and Hashtables basically the same thing???

Not really arrays only allow numerical indcies, while hashtables in lua can have ANY type of index functions, tables, strings, numbers etc…

A better way to say it is, an array is a type of hashtable, not vice versa.

Yes.

Ohh ok thanks I think i get it :slight_smile: