xTravax Posted April 19, 2015 Share Posted April 19, 2015 hey so i got a table which looks kinda like this table = { {"playerSerial1","somedata","someotherdata"}, {"playerSerial2","somedata2","someotherdata2"}, {"playerSerial3","somedata3","someotherdata3} } so if i want to check players serial and if he has some data i'd do for i,v in pairs(table) do if v[1] == serial then return true else return false end end however if let's say our specific player's serial is on index 2 in table, it won't ever be looped only one serial will be looped, others will be ignored i have those multiple datas in table and i want to get data from specific serial in table please help Link to comment
Walid Posted April 19, 2015 Share Posted April 19, 2015 try this: for i,v in pairs(table) do local type = getElementsByType("player") for index, player in pairs(type) do local serial = getPlayerSerial (player) if v[1] == serial then return v[2] or v[3] -- data here etc..... else return false end end Link to comment
xTravax Posted April 19, 2015 Author Share Posted April 19, 2015 (edited) Walid, your code does same thing as mine. edit: your code would still have a bug which i also have looping through table returns only one index of table, and doesn't check all of them so if one serial isnt same with the serial that im comparing it to, it will return false and won't check other table indexes Edited April 19, 2015 by Guest Link to comment
xTravax Posted April 19, 2015 Author Share Posted April 19, 2015 above thing is the full code (if you put this loop in a function, and send serial to function while there are multiple serials on few indexes, if only ONE index serial isn't the one we're comparing it to, it will return false and SKIP other indexes for checking if serial is in table) Link to comment
Walid Posted April 19, 2015 Share Posted April 19, 2015 (edited) Try to change your table like this : table = { [1] = {"playerSerial1","somedata","someotherdata"}, [2] = {"playerSerial2","somedata2","someotherdata2"}, [3] = {"playerSerial3","somedata3","someotherdata3"} } Edited April 19, 2015 by Guest Link to comment
xTravax Posted April 19, 2015 Author Share Posted April 19, 2015 how do i look if my serial is in table if table looks that way? (sorry i never used table with [] which contains table inside table so im not sure how to do this) Link to comment
Walid Posted April 19, 2015 Share Posted April 19, 2015 how do i look if my serial is in table if table looks that way? (sorry i never used table with [] which contains table inside table so im not sure how to do this) it can be like this : for i=1,#table do if table[i][1] == serial then --etc ... end Link to comment
ALw7sH Posted April 19, 2015 Share Posted April 19, 2015 This is much better and easier local Table = { ["Serial1"] = {data1="test1",data="test2"}, ["Serial2"] = {data1="test1",data="test2"}, } print(Table["Serial1"].data1) -- test1 print(Table["Serial2"].data2) -- test2 local MySerial = "Serial2" for serial,datas in pairs(Table) do if serial == MySerial then print(datas.data1) -- test1 for k,data in pairs(datas) do print(data) -- test1 then test2 end end end Link to comment
xTravax Posted April 19, 2015 Author Share Posted April 19, 2015 i still have same issue Link to comment
Walid Posted April 19, 2015 Share Posted April 19, 2015 i still have same issue Have you tried my code Link to comment
ALw7sH Posted April 19, 2015 Share Posted April 19, 2015 Can you explain more what you want to do? Link to comment
xTravax Posted April 19, 2015 Author Share Posted April 19, 2015 i want above table to work on same principle like if i had this table table = { ["serial1"] = true, ["serial2"] = true, ["serial3"] = true } if table[getPlayerSerial(player)] then return true else return false end Link to comment
TAPL Posted April 19, 2015 Share Posted April 19, 2015 sTable = { ["playerSerial1"] = {"somedata", "someotherdata"}, ["playerSerial2"] = {"somedata2", "someotherdata2"}, ["playerSerial3"] = {"somedata3", "someotherdata3"} } if sTable[serial] then -- print(sTable[serial][1]) -- print(sTable[serial][2]) return true else return false end Also don't use table as variable name because it can conflict with functions such as table.insert and table.remove. Link to comment
ALw7sH Posted April 19, 2015 Share Posted April 19, 2015 Do you mean if the data in the table return true ? Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now