Writing to an dictionary doesn't affect it

I’m making an animation player that works with the standard Roblox animation controller.
To play additional animations I’m using an event that can be fired from the server. Once fired, it creates an animation object and adds it to a dictionary so I can check what animations not to stop once the main movement has to stop an animation.
The actual problem,

CustomAnimations = {}
CustomAnimations["Something"] = true

for i,v in pairs(CustomAnimations) do
	print(i .."     ".. v)
end

print(#CustomAnimations)

The script prints “Something true”, as it should.
But when I print the last line, it returns 0 while it should return 1.
Why is this?

Because CustomAnimations is a dictionary, not an array. I think.
Try table.insert(CustomAnimations, {Something = true})

1 Like

The length “#” operator only works on arrays (tables whose elements are indexed by sequential integers starting at 1). To count the total number of elements in a dictionary, you would have to iterate through it yourself:

local c = 0
table.foreach(CustomAnimations, function() c += 1 end)
print(c)
1 Like