Hero192 Posted September 24, 2015 Share Posted September 24, 2015 Hey guys i m trying to do a saver , position,money and account but it doesn't works plus no error, please i need someone to give me a hand i stuck on this point, Here's my code! local connection = exports.sql:getSQLConnection() addEventHandler("onResourceStart",resourceRoot, function() dbExec(connection, "CREATE TABLE IF NOT EXISTS accountdata ( account TEXT, x TEXT, y TEXT, z TEXT, money INTEGER)") end) function saveDataBase() local account = getPlayerAccount ( source ) local money = getPlayerMoney ( source ) local x, y, z = getElementPosition ( source ) if account and not isGuestAccount(account) then local accountName = getAccountName(account) if AccountExist ( accountName ) then dbExec ( connection, "INSERT INTO accountdata (x, y, z, money, account ) VALUES ( ?, ?, ?, ?, ?, ? )", x, y, z, money, accountName) end end end function AccountExist ( AccountName ) local result = dbPoll( dbQuery ( connection, "SELECT * FROM accountdata WHERE account = ?", tostring (AccountName)),-1) if ( type ( result ) == "table" and #result == 0 or not result ) then return false else return true end end function loadDataBase ( ) local account = getPlayerAccount ( source ) local accountName = getAccountName(account) local data = AccountExist(accountName) if ( data and type ( data ) == 'table' ) then for index, val in ipairs ( data ) do if ( val['account'] == account ) then local x = tonumber ( val['x'] ) local y = tonumber ( val['y'] ) local z = tonumber ( val['z'] ) local money = tonumber ( val['money'] ) givePlayerMoney ( source, money ) setElementPosition (source, x, y, z ) end end end end addEventHandler("onPlayerLogin", root, loadDataBase) addEventHandler("onPlayerQuit", root, saveDataBase) addEventHandler("onPlayerLogout", root, saveDataBase) Link to comment
TAPL Posted September 24, 2015 Share Posted September 24, 2015 (edited) Try: local connection = exports.sql:getSQLConnection() addEventHandler("onResourceStart", resourceRoot, function() dbExec(connection, "CREATE TABLE IF NOT EXISTS accountdata (account TEXT, x TEXT, y TEXT, z TEXT, money INTEGER)") end) function saveDataBase(account) local account = eventName == "onPlayerLogout" and account or getPlayerAccount(source) if account and not isGuestAccount(account) then local money = getPlayerMoney(source) local x, y, z = getElementPosition(source) local accountName = getAccountName(account) if AccountExist(accountName) then dbExec(connection, "UPDATE accountdata SET x = ?, y = ?, z = ?, money = ? WHERE account = ?", x, y, z, money, accountName) else dbExec(connection, "INSERT INTO accountdata VALUES (?, ?, ?, ?, ?)", accountName, x, y, z, money) end end end function AccountExist(accountName) local result = dbPoll(dbQuery(connection, "SELECT x, y, z, money FROM accountdata WHERE account = ? LIMIT 1", accountName),-1) if type(result) == "table" and #result > 0 then return result[1] end return false end function loadDataBase(_, account) local data = AccountExist(getAccountName(account)) if data then setPlayerMoney(source, data["money"]) spawnPlayer(source, data["x"], data["y"], data["z"]) fadeCamera(source, true) setCameraTarget(source) end end addEventHandler("onPlayerLogin", root, loadDataBase) addEventHandler("onPlayerQuit", root, saveDataBase) addEventHandler("onPlayerLogout", root, saveDataBase) Edited September 25, 2015 by Guest Link to comment
Hero192 Posted September 24, 2015 Author Share Posted September 24, 2015 Thanks for your try but it doesn't works it returns with some errors ERROR: attempt to index local 'data' (a boolean value) at line 34 Also when i connect the code to Mysql to check the table is created succesfully but there's no columns and rows inside it Link to comment
TAPL Posted September 24, 2015 Share Posted September 24, 2015 Try delete the table and start the resource again. Link to comment
Hero192 Posted September 24, 2015 Author Share Posted September 24, 2015 Yes i did like 3 Times but with the same result Link to comment
Hero192 Posted September 25, 2015 Author Share Posted September 25, 2015 It's working thanks alot but i need to equal in the load part if the account in sqlite table equal to the player account it doesn't works if i did that, and i need this part to do something function loadDataBase(_, account) local data = AccountExist(getAccountName(account)) if data then if data["account"] == getAccountName(account) then setPlayerMoney(source, data["money"]) spawnPlayer(source, data["x"], data["y"], data["z"]) fadeCamera(source, true) setCameraTarget(source) end end end Link to comment
TAPL Posted September 25, 2015 Share Posted September 25, 2015 Why you need to do that? It already done with the SQL SELECT. Link to comment
Hero192 Posted September 25, 2015 Author Share Posted September 25, 2015 Why you need to do that? It already done with the SQL SELECT. I'll use it in other save thats why Link to comment
Hero192 Posted September 25, 2015 Author Share Posted September 25, 2015 this account from the sqlite table always returns 'nil' Link to comment
TAPL Posted September 25, 2015 Share Posted September 25, 2015 It's already been checked with SELECT, as you can see here it only select rows that have 'account' column equal to the account name and it limited to only one row as we already know there won't be more than one row for each account name. It return nil because i didn't select the account name as it not needed, you already know it as you have pass it to the SQL query. Link to comment
Hero192 Posted September 25, 2015 Author Share Posted September 25, 2015 It's already been checked with SELECT, as you can see here it only select rows that have 'account' column equal to the account name and it limited to only one row as we already know there won't be more than one row for each account name. It return nil because i didn't select the account name as it not needed, you already know it as you have pass it to the SQL query. Now, i understand well thanks again and for your time to explain this Link to comment
Hero192 Posted September 25, 2015 Author Share Posted September 25, 2015 Thanks alot i understood many things from you just now and thanks for helping via pm this topic can be locked 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