Table when printed is "empty", not sure why

local loaded_anims = {}

local movement_anims = {
	["default"] = {
		idle = "asset1",
		walk = "asset2",
		run = "asset3",
		jump = "asset4",
		fall = "asset5",
	}
}

local function animationSetup()
	for animPack, packTable in pairs (movement_anims) do
		for animAction, animId in pairs(packTable) do
			
			local animationObject = Instance.new("Animation")
			animationObject.Name = animAction
			animationObject.AnimationId = animId
			
			loaded_anims[animPack] = animAction[animationObject]
		end
	end
end

animationSetup()

task.wait(1)
print(loaded_anims) -- {}

I don’t get why animationSetup is a function. You should consider removing it, unless its needed for other purposes.

The reason behind why the table is empty is because movement_anims is a dictionary. You cannot loop it using in pairs.

You should use pairs for dictionaries lol

how? it looped through the dictionary though, [“default”] and the table assigned to it

Ok, but since dictionaries have no specific order, the i in for i, v in pairs() should not represent the actual position of the index of the dictonary

Yes… its key, value and not index, value

Can you try adding a print statements inside the function like this

local function animationSetup()
	for animPack, packTable in pairs (movement_anims) do
        print(animPack,packTable)
		for animAction, animId in pairs(packTable) do
			print(animAction,animId)
			local animationObject = Instance.new("Animation")
			animationObject.Name = animAction
			animationObject.AnimationId = animId
			
			loaded_anims[animPack] = animAction[animationObject]
            print(loaded_anims)
		end
	end
end

it helps, look at the values of the prints then you’ll know where you went wrong

sorry I forgot, brain not functioning well today

image
not sure why the table is still blank, even though the print for the animAction and animId is fine

hold up, why is the value equal to animAction? I think you did something wrong here

animAction is not a table (its walk, run, fall). I don’t know how you want to sort the table, so just take this into consaderation.

I don’t know if this is exactly what you wanted, but you can try this:

loaded_anims[animPack] = {}
table.insert(loaded_anims[animPack], animationObject)

Replace last line in the function with this.
Edit: No, this won’t work.

local loaded_anims = {}

local movement_anims = {
	["default"] = {
		idle = "asset1",
		walk = "asset2",
		run = "asset3",
		jump = "asset4",
		fall = "asset5",
	}
}

local function animationSetup()
	for animPack, packTable in pairs (movement_anims) do
	    loaded_anims[animPack] = {}
	    
		for animAction, animId in pairs(packTable) do
			
			local animationObject = Instance.new("Animation")
			animationObject.Name = animAction
			animationObject.AnimationId = animId
			
			loaded_anims[animPack][animAction] = animationObject
		end
	end
end

animationSetup()

task.wait(1)
print(loaded_anims) -- {}
1 Like

wha how wha- if its fine with you but can you please point out the errors as to how did i screw up in my code and maybe a little explanation?