Datatables

Discussion in 'Rust Development' started by CriticalmAss, Jan 25, 2015.

  1. Hey folks, quick question. Well, maybe not that quick.

    I'm trying to setup tables and write a file when a player connects. I can create teh file with no problem but if I throw an if then in to only create default fields if a STEAMID = nil, it just skips over the if. If I pull out the if then, it just creates the default values whether the field is < default value or not.

    In general I'm trying to do the following 2 things.
    1. Load the data if it exists
    2. Load default values of 0 for deaths if the STEAMID does not exist
    3. Do not overwrite values if they already exist.

    Thanks for any help you can offer.
    Code:
    function PLUGIN:Init()
        self.table = datafile.GetDataTable("deaths")
      
    endfunction PLUGIN:OnPlayerConnected(packet)
        self.table = datafile.GetDataTable("deaths")
        if not packet then
            return
        end
        if not packet.connection then
            return
        end
        local STEAMID = rust.UserIDFromConnection(packet.connection)
        self.table.Players = self.table.Players or {}
        if (self.table.Players[STEAMID] == nil) then
            local deaths = 0
            self.table.Players[STEAMID] = self.table.Players[STEAMID] or {
                ["Username"] = packet.connection.username,
                ["Deaths"] = deaths
            }
        end
    end
     
  2. Try this
    Code:
    function PLUGIN:Init()
        self.table = datafile.GetDataTable("deaths") or {}
    endfunction PLUGIN:OnPlayerConnected(packet)
        if not packet then
            return
        end
        if not packet.connection then
            return
        end
        local STEAMID = rust.UserIDFromConnection(packet.connection)
        self.table.Players = self.table.Players or {}
        if (self.table.Players[STEAMID] == nil) then
            local deaths = 0
            self.table.Players[STEAMID] = {
                ["Username"] = packet.connection.username,
                ["Deaths"] = deaths
            }
        end
        datafile.SaveDataTable("deaths")
    end
     
  3. If you edit the deaths data table and put a 1 in for deaths, on the next user connect, it still erases that data and puts the default 0 back in the file.
     
  4. Of course it does.
    The plugin doesnt know when you edit the file because its read when the server init and then the plugin works with the data loaded from that. If you edit the data manually in the file the plugin still uses the data in the ram.
    If you edit the file manually and then reload the plugin it should recognize the change because its loading the file again.
     
  5. Ugg...

    Thanks for slapping my brain. So obvious too.