Page 1 of 1

Can't Change Table Value

PostPosted: Thu Aug 18, 2016 17:49
by octacian
I'm still in the midst of finishing a rank "module" for my servertools mod (will be released after this). My current problem is that when a player's rank is changed, I need to update it in a file containing a table (file is rank.db, table is called "mem"). Though the rest of the process completes just fine, I can't get it to change the rank in the table.

Issue:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
for _,i in ipairs(mem) do
    if i.name == name then
      mem[_].rank = newrank -- set new rank value
      datalib.table.write(modpath.."/rank.db", mem) -- write updated table
      servertools.log(name.."'s rank set to "..newrank..".")
    end
  end


Full set player rank function:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- set rank
function servertools.set_player_rank(name, newrank)
  -- check params
  if not minetest.get_player_by_name(name) then
    return "Invalid player: "..name
  elseif not newrank then
    return "New rank field required."
  end

  local rank_privs = servertools.get_rank_privs(newrank) -- get rank privileges
  -- check that rank exists
  if not rank_privs or type(rank_privs) ~= "table" then
    return "Rank nonexistent."
  end

  -- update player privileges
  if not minetest.set_player_privs(name, rank_privs) then return false end -- unsuccessful
  -- TODO: fix setting table value
  -- update database
  for _,i in ipairs(mem) do
    if i.name == name then
      mem[_].rank = newrank -- set new rank value
      datalib.table.write(modpath.."/rank.db", mem) -- write updated table
      servertools.log(name.."'s rank set to "..newrank..".")
    end
  end
end


RankDB (example):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
return {{["name"] = "singleplayer", ["rank"] = "player"}, {["name"] = "octacian", ["rank"] = "owner"}, {["name"] = "oct", ["rank"] = "player"}}


mem table (example):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
mem = {
    { name = "singleplayer", rank = "player" },
    { name = "octacian", rank = "owner" },
    { name = "oct", rank = "player" },
}


I was thinking about removing the record, then inserting a new one, but that should definitely not be required.

Thanks.

Re: Can't Change Table Value

PostPosted: Fri Aug 19, 2016 11:35
by srifqi
Have you tried to log the new mem[_] ? Does it's value change?

Re: Can't Change Table Value

PostPosted: Fri Aug 19, 2016 11:38
by rubenwardy
Try replacing _ with key?