A system to reference instances across the client/server boundary using Instance.UniqueId and a new method in CollectionService

Problem

As a Roblox developer, it is currently too hard to reference instances across the client/server boundary using RemoteEvent.

Discussion

Since instances cannot be sent across the client/server boundary, and difficult to send instance references, I propose using Instance.UniqueId to solve this issue. Currently, this field is locked to Roblox use only. The field is marked as read parallel so it’s safe to use in a multi-threading environment with Actor.

Implementation

A proposed implementation would be that all instances with a valid UniqueId would have their references placed in a hash table for fast lookups. So when the developer wants to reference an instance, they read the UniqueId field, send to over the network via RemoteEvent, and the other side checks their copy of the hash table and returns the actual instance, or nil if it’s not found. As instances are parented to or removed from the data model or destroyed, the hash table is updated. The hash table would map the hash key to a struct containing the unique Id and the instance reference so the table is always up to date. A proposed C struct is provided below:

struct instanceHashRecord_st {
	unsigned char[16] uniqueId;
	Instance *ptr;
	instHashRec_t *next;
	instHashRec_t *prev;
}
typedef struct instanceHashRecord_st instHashRec_t

The struct is defined for a doubly linked list for use in a chained hash table. The lookup can be handled with CollectionService. A Method such as CollectionService:GetInstanceFromUniqueId(UniqueId: string): Instance | nil would work. The passed UniqueId string would be either a 32-byte alphanumeric hex string or a 16-byte binary string. The method would return either the instance, or nil if the instance does not exist. This would not work for nil parented instances. Only instances parented somewhere in the data model would be on the hash list. This will take the complexity of creating and maintaining a dictionary table out of the developer’s purview and LUA code and place hash table operations squarely in C code which will be many times faster than a pure LUA implementation.

Furthermore, by using such a setup, there will be no further need for developers create their own unique instance Id system.

Use Cases

The following use cases are presented.

Use Case 1

Background

For games with streaming enabled, the client needs to know when a set of instances have been loaded.

Use Case

A player is playing a game that uses streaming. There are instances that form a building that the player must enter to perform some task. With streaming mode enabled, it is not guaranteed that the required instances have been loaded on the client. Using such a system, the client can check if the UniqueId of those instances are present or not. If not, then the client can pause and wait for the required instances to load before allowing the player to proceed.

Use Case 2

Background

The client is not sure of where an instance is located.

Use Case

By using the proposed system, the client can query the server for the UniqueId of the instance and get it directly. What the client needs the instance for is dependent on the game.

Use Case 3

Background

Client code that refers to specific instances by name and location must be updated if the locations of those instances changes on the server.

Use Case

Related to Use Case 2 above, code that expects instances to be in specific locations is brittle. If the location on the server changes, then the change also must be applied to the code on the client that references it. By using this proposed system, this will reduce developer overhead when writing code since the changes on the server will automatically be reflected on the client because the client is using UniqueId instead of name and location of instances.

Use Case 4

Background

Names of instances are not unique.

Use Case

As stated in the Roblox documentation, the names of instances are not unique. Implementing this feature will relieve the developer’s burden of having to figure out which instance they want in a list of instances of the same name and class without having to assign some unique attribute and search for it. As a side effect, the entire for loop construct (as shown below) can be eliminated completely thereby significantly increasing performance of the code.

local ident: string = "abcd1234"
local object: Instance
for _, item: Instance in ipairs(game.Workspace.SomeInstance:GetChildren()) do
	if item:IsA("Some Class") and item.Name == "Some Name" then
		objIdent = item:GetAttribute("UniqueId")
		if type(objIdent) == "string" and objIdent == ident then
			object = item
			break
		end
	end
end

Additional Information

This feature is sorely needed as there are multiple, developer created implementations out there that try to work around the issues stated in this feature request. I realize this has been requested before, but I believe a fresh take is warranted.

Conclusion

If Roblox is able to address this issue, it would improve my development experience because I would no longer need to implement a slow and clunky unique Id system that is prone to errors. By sending the UniqueId of an instance across the network, and using the proposed CollectionService method, the referencing of instances across the client/server boundary would be made significantly simpler and have increased performance in some use cases.

bummmmmpppppppppp important stuff