How do I unpack this table?

For reference, variable BS =

	Blood = {
		Enabled = true;
			A = { -- Main
				Amt_Min = 		3;
				Amt_Max = 		10;
				Speed = 		50;
				Spread = 		{-15,15};
				Size = 			2; -- Pool Size
				WidthScale =	1.5;
				UpVector = 		Vector3.new(0,0,0);
				RepeatCount =	1; -- Amount of times to repeat
				RepeatTick =	0; -- Wait time between ticks
			};
			B = { -- Bleed out effect
				Amt_Min = 		1;
				Amt_Max = 		1;
				Speed = 		5;
				Spread = 		{-15,15};
				Size = 			0.5; -- Pool Size
				WidthScale =	0.4;
				UpVector = 		Vector3.new(0,0,0);
				RepeatCount =	5; -- Amount of times to repeat
				RepeatTick =	0.5; -- Wait time between ticks
			};
	}

In one local script:

		Events2.BloodTrailEvent:Fire(
				a, -- attachment
				unpack(BS.A)
			)

In another:

local function BloodTrailFunc(Attachment,Amount,Speed,spread,size,widthsize,upvector,tim,tic)
	print(Amount,Speed,spread,size,widthsize,upvector,tim,tic)

This just returns nils, how do I get around this, I am not so familiar with how unpacking works.

2 Likes

What unpack does is return a collection of values from an array, which you can assign to multiple variables, like so:

local array = {1, 2, 3}

local one, two, three = unpack(array)

print(unpack(array)) --> 1 2 3
print(one, two, three) --> 1 2 3

This does not work with a dictionary like yours because it’s simply not an array. You can just return the dictionary itself and index from it when you need a value.
e.g:

Events2.BloodTrailEffect:Fire(
    a, -- attachment
    BS.A
)
local function BloodTrailFunc(Attachment, bsa)
    local Spread = bsa.Spread
    local size = bsa.Size
    -- ...
    print(--[[your variables]])

    -- or
    
    table.foreach(bsa, print)
end
4 Likes

Yeah, seems I had to do that. I just wondered if there was a way to unpack because the way the server requested the bloodtrailfunc was different to how I was doing it on the client.