Transfer tables values

I have created a class to to create a basic dominoes set, and like to distribute the pieces among the quantity of players. My “dominoes.lua” file have two functions

[code]function shuffle(t)
– Shuffles the pieces
end

function dominoesSet()
– code to create the set, an table with 28 pieces
– Every index of my table holds some piece information, like an image, a value, etc.
– For example: “set[1] = {value1 = 1, value2 = 5, …}”

return set;
end
[/code]

After this, in my “main.lua”, I’ve created an object to get the set:

local dominoes = require("dominoes");  
local set = dominoes.dominoesSet();  

Everything is fine at this point, but i want to make other tables to separate the pieces for the players, I created a function, in main, to get 7 pieces from the set and copy them to a new table:

function players(n, set)  
 if ( players == 1) then  
 p1 = {};  
 for i = 1, 7 do  
 p1[i] = set[i];  
end  
  
-- in main  
local p1 = players(1, set);  

All seems to be simple, but I get error in the code when I try to print any value of this new table. For example:

print (set[1].value) -- works  
print (p1[1].value) -- get an error  

What I’m missing? If I delete the second print line, It’s all ok…

Thanks!!! [import]uid: 54349 topic_id: 13026 reply_id: 313026[/import]

it’s very simple, you are NOT returning p1 from your function but you are assigning the return value to p1. Secondly, the ; is not a substitute for end like in C/C++

use
[lua]function players(n, set)
if ( players == 1) then
p1 = {};
end
for i = 1, 7 do
p1[i] = set[i];
end

return p1
end

– in main
local p1 = players(1, set);[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13026 reply_id: 47825[/import]

Thank you, jayantv! In my first attempt I was returning the value. I must have remove it and didn’t realize that. Other confusion, I was using “n” in the value of players and testing “players” instead.

Everything is okay now:

function players(n, set)  
 if ( n == 1) then -- it was "if ( players == 1) then"  
 p1 = {};  
 for i = 1, 7 do  
 p1[i] = set[i]  
  
 return p1   
end  
   
-- in main  
local p1 = players(1, set)  

About the “;”. Is really hard to not use this for me. My background is all about C and some C++.

Thanks!!! [import]uid: 54349 topic_id: 13026 reply_id: 47888[/import]