Why cant i print the contents of this table?

local Event = game.ReplicatedStorage:WaitForChild("ShopDataSender")
local Money = game.ReplicatedStorage:WaitForChild("SharedCash")

Event.OnServerEvent:Connect(function(plr, Cart, Price)
	for I, V in Cart do
		print("Buying "..V[I].Value.Name)
	end
	print(Price)
end)

Im not trying to print the table, im trying to print its contents individually.
Idk how to use for loops unless its for _, V. I have never used for I, V before.
i just know I stands for index and V stands for value.

Disable Log Mode in the Output options.

2 Likes

I and V in your example basically represent the key and value of the table. For example, with the following:

local Table = {
    Key1 = "Value1",
    Key2 = "Value2",
    Key3 = "Value3"
}

for Key, Value in Table do
    print(Key) -- Will print "Key1", "Key2", "Key3"
    print(Value) -- Will print "Value1", "Value2", "Value3"
end

Arrays are just a special kind of key-value table where the keys are indices 1 - N

local Array= { "Value1", "Value2", "Value3" }

for Index, Value in Array do
    print(Index) -- Will print 1, 2, 3
    print(Value) -- Will print "Value1", "Value2", "Value3"
end 

I don’t know the structure of Cart in your example, but doing V[I] does look off and might be the issue for you.

1 Like

Oh gee, my bad. I didn’t read the issue thoroughly. My fault :sweat_smile:

I don’t think you can use i, v loops for tables. try doing a different loop:

local Event = game.ReplicatedStorage:WaitForChild("ShopDataSender")
local Money = game.ReplicatedStorage:WaitForChild("SharedCash")

Event.OnServerEvent:Connect(function(plr, Cart, Price)
	for i = 1, #Cart, 1 do -- << Loops Through Each Part in Cart
		print("Buying ".."Whatever You Want To Print Here")
	end
	print(Price)
end)

You can most definitely use i, v loops for tables. Those loops just iterate over the keys and values of the table.

it wont work…
here is an in-depth description of it:
Cart is a table that is full of nothing but object values.
Im trying to print out each of the object value’s object names.

so for example
ObjectVal1.Value = “Model1”
ObjectVal2.Value = “Model2”

what i want is to print out objectval 1 and 2’s values name.
So i want this to show up in the console:
Buying Model1
Buying Model2

NOT
Buying Model1 Model2

Thanks if you understand!

im gonna make a new post, i tihnk this one is dead