RastaOrecha Posted May 26, 2013 Share Posted May 26, 2013 How to get arguments from the toJSON in fromJSON from the table the query sql? local guns = toJSON ( { gun0=getPedWeapon(source,0),ammo0=getPedTotalAmmo(source,0), gun1=getPedWeapon(source,1),ammo1=getPedTotalAmmo(source,1) } ) local save = dbQuery( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", guns, getPlayerName(source) ) dbFree( save ) Now, how to retrieve data and to give the player weapons? Link to comment
DiSaMe Posted May 26, 2013 Share Posted May 26, 2013 Since it's not magic, it doesn't matter how you stored the value, that's how programming works. To get the data from MySQL, use SELECT statement in dbQuery and then dbPoll to get the result. Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 That's what I'm doing, I do not know how to assign a variable value from JSON cell Link to comment
Arimance Posted May 26, 2013 Share Posted May 26, 2013 How to get arguments from the toJSON in fromJSON from the table the query sql? local guns = toJSON ( { gun0=getPedWeapon(source,0),ammo0=getPedTotalAmmo(source,0), gun1=getPedWeapon(source,1),ammo1=getPedTotalAmmo(source,1) } ) local save = dbQuery( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", guns, getPlayerName(source) ) dbFree( save ) Now, how to retrieve data and to give the player weapons? local save = dbQuery( hconnect, "select Guns from users WHERE `Name`=?", getPlayerName(source) ) local poll = dbPoll( save, -1 ) local gunz = fromJSON( poll[1].Guns ) Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 How to use next? gunz.gun0 gunz.ammo0 ??? Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 (edited) Guns are not issued, I use this: local ld = dbQuery(hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username) local result = dbPoll(ld, -1 ) local gunz = fromJSON( result[1].Guns ) giveWeapon(source, gunz.gun0, gunz.ammo0) Edited May 26, 2013 by Guest Link to comment
Castillo Posted May 26, 2013 Share Posted May 26, 2013 You can use loops to make it easier. Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 You can use loops to make it easier. Can you give an example, please? Link to comment
Castillo Posted May 26, 2013 Share Posted May 26, 2013 local guns = { } for slot = 0, 12 do guns [ slot ] = { gun = getPedWeapon ( source, slot ), ammo = getPedTotalAmmo ( source, slot ) } end local save = dbExec ( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", toJSON ( guns ), getPlayerName ( source ) ) Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 local guns = { } for slot = 0, 12 do guns [ slot ] = { gun = getPedWeapon ( source, slot ), ammo = getPedTotalAmmo ( source, slot ) } end local save = dbExec ( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", toJSON ( guns ), getPlayerName ( source ) ) This is a insert and how to extract? Link to comment
Castillo Posted May 26, 2013 Share Posted May 26, 2013 local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) local result = dbPoll ( ld, -1 ) local guns = fromJSON ( result [ 1 ].Guns ) if ( type ( guns ) == "table" ) then for _, weapon in pairs ( guns ) do if ( weapon.gun and weapon.ammo ) then giveWeapon ( source, weapon.gun, weapon.ammo ) end end end Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) local result = dbPoll ( ld, -1 ) local guns = fromJSON ( result [ 1 ].Guns ) if ( type ( guns ) == "table" ) then for _, weapon in pairs ( guns ) do if ( weapon.gun and weapon.ammo ) then giveWeapon ( source, weapon.gun, weapon.ammo ) end end end attempt to index local 'weapon' (a number value) on: if ( weapon.gun and weapon.ammo ) then Link to comment
Castillo Posted May 26, 2013 Share Posted May 26, 2013 Are you sure that it saved the way I gave you? Link to comment
RastaOrecha Posted May 26, 2013 Author Share Posted May 26, 2013 Are you sure that it saved the way I gave you? Yeah Save: local guns = { } for slot = 0, 5 do guns [ slot ] = { gun = getPedWeapon ( source, slot ), ammo = getPedTotalAmmo ( source, slot ) } end local save = dbExec ( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", toJSON ( guns ), getPlayerName ( source ) ) dbFree( save ) Load: local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) local result = dbPoll ( ld, -1 ) local guns = fromJSON ( result [ 1 ].Guns ) if ( type ( guns ) == "table" ) then for _, weapon in pairs ( guns ) do if ( weapon.gun and weapon.ammo ) then giveWeapon ( source, weapon.gun, weapon.ammo ) end end end Link to comment
Castillo Posted May 26, 2013 Share Posted May 26, 2013 Show me the data which is saved on SQL. Link to comment
RastaOrecha Posted May 27, 2013 Author Share Posted May 27, 2013 Why you ignore castillo's post ? Yes we can help you?! I don't know what happens to people these days . I'm sorry, I didn't see the 2nd page SQL DB: Guns: [ { "ammo0": 1, "gun4": 32, "gun2": 22, "gun5": 30, "ammo3": 0, "gun3": 0, "gun0": 0, "gun1": 0, "ammo1": 0, "ammo5": 120, "ammo4": 64, "ammo2": 90 } ] Link to comment
Castillo Posted May 27, 2013 Share Posted May 27, 2013 That's not generated by the code I gave you. Link to comment
RastaOrecha Posted May 28, 2013 Author Share Posted May 28, 2013 That's not generated by the code I gave you. [ { "1": { "ammo": 0, "gun": 0 }, "2": { "ammo": 90, "gun": 24 }, "3": { "ammo": 0, "gun": 0 }, "4": { "ammo": 0, "gun": 0 }, "5": { "ammo": 90, "gun": 30 }, "0": { "ammo": 1, "gun": 0 } } ] Now there is no error, but the weapon isn't issued Link to comment
Castillo Posted May 28, 2013 Share Posted May 28, 2013 local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) local result = dbPoll ( ld, -1 ) local guns = fromJSON ( result [ 1 ].Guns ) if ( type ( guns ) == "table" ) then for _, weapon in pairs ( guns ) do if ( weapon.gun and weapon.ammo ) then outputChatBox ( tostring ( weapon.gun ) ..", ".. tostring ( weapon.ammo ) ) giveWeapon ( source, weapon.gun, weapon.ammo ) else outputChatBox ( "No weapon data" ) end end end See what it returns. Link to comment
RastaOrecha Posted May 29, 2013 Author Share Posted May 29, 2013 local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) local result = dbPoll ( ld, -1 ) local guns = fromJSON ( result [ 1 ].Guns ) if ( type ( guns ) == "table" ) then for _, weapon in pairs ( guns ) do if ( weapon.gun and weapon.ammo ) then outputChatBox ( tostring ( weapon.gun ) ..", ".. tostring ( weapon.ammo ) ) giveWeapon ( source, weapon.gun, weapon.ammo ) else outputChatBox ( "No weapon data" ) end end end See what it returns. [ { "1": { "ammo": 0, "gun": 0 }, "2": { "ammo": 90, "gun": 24 }, "3": { "ammo": 0, "gun": 0 }, "4": { "ammo": 0, "gun": 0 }, "5": { "ammo": 90, "gun": 30 }, "0": { "ammo": 1, "gun": 0 } } ] Output: 0, 0 0, 1 24, 90 30, 90 0, 0 Link to comment
Castillo Posted May 29, 2013 Share Posted May 29, 2013 Well then, it has to work, these are the correct weapon ID's and ammo. Are you sure that 'source' is the right player element? Link to comment
RastaOrecha Posted May 29, 2013 Author Share Posted May 29, 2013 Well then, it has to work, these are the correct weapon ID's and ammo.Are you sure that 'source' is the right player element? Yes, I'm sure. Maybe the problem ain't in this code? It appears from other resources ain't issued weapons, too. For example: giveWeapon(source, 24, 500, true) What can be the reason? Link to comment
Castillo Posted May 29, 2013 Share Posted May 29, 2013 You can't give yourself weapons with any other way, that's what you mean? 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