Sorry to bump this thread, but I want to provide more context to the test in the original post as its a bit biased towards a single use-case and a tad misleading. I don’t disagree with the opinion of the original poster, but I have different reasons for using alternatives to SetAttribute.
The test in the original post assumes that the data you are setting / using only exists on the server, in which case yes, storing data in a table would be the fastest and most efficient method. However, SetAttribute
comes with a caveat, and that is the fact that setting an attribute on the instance replicates to the clients - just like any other property on an instance would. So let’s say you want to use this stored information on the client for display purposes. The correct code snippet that compares both SetAttribute
and the table storage with RemoteEvents
for network replication would actually look more like the following…
local AmazingDictionary = {
Health = 0,
Speed = 0,
}
local AttributeHolder = Instance.new("Part",workspace)
local AttributeTime = os.clock()
for i = 1,10000 do
AttributeHolder:SetAttribute("Health",i)
AttributeHolder:SetAttribute("Speed",i)
end
print(os.clock() - AttributeTime)
local DictionaryTime = os.clock()
for i = 1,10000 do
AmazingDictionary.Health = i
game.ReplicatedStorage.RemoteEvent:FireAllClients({
health = AmazingDictionary.Health
})
AmazingDictionary.Speed = i
game.ReplicatedStorage.RemoteEvent:FireAllClients({
speed = AmazingDictionary.Speed
})
end
print(os.clock() - DictionaryTime)
Doing this we see that SetAttribute
is around 30ms
and table with RemoteEvents
is around 55ms
.
for i = 1,10000 do
AmazingDictionary.Health = i
AmazingDictionary.Speed = i
game.ReplicatedStorage.RemoteEvent:FireAllClients({
speed = AmazingDictionary.Speed,
health = AmazingDictionary.Health
})
end
Further optimizing the RemoteEvents
method to combine all property changes into a single RemoteEvent call reduces the time to about 30ms
, the same as two SetAttribute calls. Which means that RemoteEvents become more efficient as more data is combined into a single network call (given the amount of data being set in both methods is the same size).
So in conclusion, don’t use SetAttribute
if your plan is to only use the values on the server and you’re doing this in large quantities. It is creating needless network events which will start to cause network bottleneck, and eventually FPS drops. Small quantities of SetAttribute is negligible and will cause no noticeable impact to your game’s network performance. If you plan to utilize this data on the client in some way, it would be acceptable to use SetAttribute for a small quantities of values (e.g. health, speed, damage, state), but anything over 3-4 will start to show a noticeable difference when compared to RemoteEvents. Personally, I prefer RemoteEvents as they are more customizable with who the data is replicated to, but SetAttribute can be a convenient and easy alternative in the right circumstances.