Jump to content

sql save


Recommended Posts

Posted

hey, i want to save player weapons,ammo in sqlite and then when he logins in he will get them back, here is my code so far.

local slotT = "slot"
local ammoT = "ammo"
 
function start ()
executeSQLCreateTable(slotT, "accountname TEXT, slot INT")
executeSQLCreateTable(ammoT, "accountname TEXT, ammo INT")
outputDebugString ( "Resource loaded.", 3 )
end
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start)
 
function savePlayerData()
local acc = getPlayerAccount(source)
if not isGuestAccount(acc) then
local i = 0
while i < 13 do
local temp = getPlayerWeapon(source, i)
executeSQLUpdate(slotT, "slot = '"..temp.."'", "accountname = '"..getAccountName(acc).."'")
local temp2 = getPlayerTotalAmmo(source, i)
executeSQLUpdate(ammoT, "ammo = '"..temp2.."'", "accountname = '"..getAccountName(acc).."'")
           i = i +1
end
end
end
addEventHandler("onPlayerQuit", getRootElement(), savePlayerData)
addEventHandler("onPlayerLogout", getRootElement(), savePlayerData)
addEventHandler("onPlayerWasted", getRootElement(), savePlayerData)
 
 
function login1(prev, account, autologin)
local account = getPlayerAccount(source)
local result = executeSQLQuery("SELECT slot FROM slot WHERE accountname='" .. getAccountName(account) .. "'")
local result2 = executeSQLQuery("SELECT ammo FROM ammo WHERE accountname='" .. getAccountName(account) .. "'")
if result and #result > 0 then
--giveWeapon(source, result, result2)
giveWeapon(source, result, tonumber(result2))
end		   
outputDebugString(tostring(executeSQLInsert(slotT, "'0', '"..getAccountName(account).."'", "slot, accountname")))
outputDebugString(tostring(executeSQLInsert(ammoT, "'0', '"..getAccountName(account).."'", "ammo, accountname")))
end
addEventHandler("onPlayerLogin", getRootElement(), login1)
addEventHandler("onPlayerSpawn", getRootElement(), login1)

btw this code is creating somthing called

ERROR: ERROR: Infinite/too long execution (weapon)

ERROR: Aborting; infinite running script

Posted
Your while loop is infinite. I suggest you use for loop instead.

but the save weapon part is it fine or is buggi?

the code should look like this then?

local slotT = "slot"
local ammoT = "ammo"
 
function start ()
executeSQLCreateTable(slotT, "accountname TEXT, slot INT")
executeSQLCreateTable(ammoT, "accountname TEXT, ammo INT")
outputDebugString ( "Resource loaded.", 3 )
end
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start)
 
function savePlayerData()
local acc = getPlayerAccount(source)
if not isGuestAccount(acc) then
local i = 0
for i < 13 do
local temp = getPlayerWeapon(source, i)
executeSQLUpdate(slotT, "slot = '"..temp.."'", "accountname = '"..getAccountName(acc).."'")
local temp2 = getPlayerTotalAmmo(source, i)
executeSQLUpdate(ammoT, "ammo = '"..temp2.."'", "accountname = '"..getAccountName(acc).."'")
           i = i +1
end
end
end
addEventHandler("onPlayerQuit", getRootElement(), savePlayerData)
addEventHandler("onPlayerLogout", getRootElement(), savePlayerData)
addEventHandler("onPlayerWasted", getRootElement(), savePlayerData)
 
 
function login1(prev, account, autologin)
local account = getPlayerAccount(source)
local result = executeSQLQuery("SELECT slot FROM slot WHERE accountname='" .. getAccountName(account) .. "'")
local result2 = executeSQLQuery("SELECT ammo FROM ammo WHERE accountname='" .. getAccountName(account) .. "'")
if result and #result > 0 then
--giveWeapon(source, result, result2)
giveWeapon(source, result, tonumber(result2))
end		   
outputDebugString(tostring(executeSQLInsert(slotT, "'0', '"..getAccountName(account).."'", "slot, accountname")))
outputDebugString(tostring(executeSQLInsert(ammoT, "'0', '"..getAccountName(account).."'", "ammo, accountname")))
end
addEventHandler("onPlayerLogin", getRootElement(), login1)
addEventHandler("onPlayerSpawn", getRootElement(), login1)

Posted
No, it shouldn't. Learn to use google for :o sake...

for i=0,12 do
 
end

ok, but it gives a error when trying to give weapon

weapon.lua: Bad argument @ "giveWeapon" - Line: 35

Posted
Instead of
giveWeapon(source, result, tonumber(result2)

Try

giveWeapon(source, result[1]['slot'], result2[1]['ammo'])

this works but itsnt saving all weapons :S

only saves player current weapon not all slots

Posted
As far as I know
result[1]['slot']

is an outdated syntax.

Use

result[1].slot

instead of.

but still not saving all or loading them...

Posted
only saves player current weapon not all slots

getPlayerWeapon has the 'slot' parameter server side only in 1.0.4 MTA version. I guess you are using an older version, so this function takes only current weapon.

Posted
only saves player current weapon not all slots

getPlayerWeapon has the 'slot' parameter server side only in 1.0.4 MTA version. I guess you are using an older version, so this function takes only current weapon.

oh i see, then if i use a 1.0.4 version beta will work?

Posted
Yes.

i installed it and now dosnt even save his current :cry:

You can get all player weapons in client-side script and send them to the server with triggerServerEvent or you can use setElementData which can sync the data but is unsafe.

Posted
Yes.

i installed it and now dosnt even save his current :cry:

You can get all player weapons in client-side script and send them to the server with triggerServerEvent or you can use setElementData which can sync the data but is unsafe.

well, a friend had this script using account data and worked all saving all without a client part so ?

Posted

So, you have a chance to make it work.

I'm worried that cheaters' weapons aren't synced but getPedWeapon client-side will get these weapons and if you save them then they'll have "cheated" weapons saved. Maybe that's what he meant..

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...