-1

I have been trying for a month to try and return a value from a callback function using this module.

I am overwriting the default GetPData function which retrieves the data from a local sql database, but I wanted to retrieve it from a MySQL database server.

Here is the original function:

--[[---------------------------------------------------------
GetPData
- Saves persist data for this player
-----------------------------------------------------------]]  
function meta:GetPData( name, default )

    name = Format( "%s[%s]", self:UniqueID(), name )
    local val = sql.QueryValue( "SELECT value FROM playerpdata WHERE infoid = " .. SQLStr(name) .. " LIMIT 1" )
    if ( val == nil ) then return default end

    return val

end

Here is the code I tried:

function Player:GetPData(key, default)

   testget(self,key,default,function(junk)
    //LocalPlayer():ChatPrint("You have $" .. junk .. ".");
    print(junk)
    return junk
   end)

end

function testget(self,key, default, callback)
    local retVal = default
    insertQ = PDataDB:query("SELECT `value` from `playerdata` WHERE `uniqueid`='" .. self:UniqueID() .. "' AND key` = '" .. key .. "';");
    insertQ.onError = DBError
    insertQ.onData = function(self, data)
        retVal = data.value or default
        print(retVal)
        print("--------------------------------------------------------------------------------------------------------------")
    insertQ.onSuccess = function(self)
        print(retVal)
        callback(retVal);
    end
    insertQ:start()
    end

end

This is for a game called garrys mod. I need to keep track of xp all over my servers.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183

1 Answers1

1

Modify your function to something like:

function Player:GetPData( key, default )
    local retVal = 0 -- or any other default value you want
    testget(self, key, default, function(junk)
        //LocalPlayer():ChatPrint("You have $" .. junk .. ".");
        print(junk)
        retVal = junk
        return junk
        end
    )
    return retVal
end
hjpotter92
  • 78,589
  • 36
  • 144
  • 183