Jump to content

[HELP] MYSQL


itHyperoX

Recommended Posts

Hi!

I just started learning mysql.

So my problem is, i can save the player's weapon's, but when player die, the weapons just disappearing.

 

I downloaded a script from the community, which is saving weapons to *setAccountData*, and i want to use this with mysql.

 

-- I want to use something like this
local Account = mysql:singleQuery("SELECT * FROM accounts WHERE AccountID=?, Weapons=? LIMIT 1", getElementData(source, "Player:AccountID"))


addEventHandler("onPlayerSpawn",root,function ()
    local account = getPlayerAccount(source)     -- I don't know how to change this to mysql.
    if (account) and not (isGuestAccount(account)) then
        local weapons = getAccountData(account, "PlayerAccount:Weapons")   -- I don't know how to change this to mysql.
        if (weapons) and (fromJSON(weapons)) then
            takeAllWeapons(source)
            for weapon, ammo in pairs(fromJSON(weapons)) do
                giveWeapon(source, weapon, ammo, true)
            end
        end
    end 
end)
addEventHandler("onPlayerWasted",root,function ()
    local account = getPlayerAccount(source)  -- I don't know how to change this to mysql.
    if (account) and not (isGuestAccount(account)) then
        local weapons = getAllPedWeapon(source)
        setAccountData(account, "PlayerAccount:Weapons", toJSON(weapons))   
    end
end)
addEventHandler("onResourceStart",resourceRoot,function (resource)
    for _, thePlayer in pairs(getElementsByType("player")) do
        local account = getPlayerAccount(thePlayer)
        if (account) and not (isGuestAccount(account)) then
            triggerEvent("loadPlayerDatas", thePlayer, account)
        end
    end
end)
addEventHandler("onPlayerQuit",root,function ()
    local account = getPlayerAccount(source)
    if (account) and not (isGuestAccount(account)) then
        triggerEvent("savePlayerDatas", source, account)
    end
end)
addEventHandler("onResourceStop",resourceRoot,function (resource)
    for _, thePlayer in pairs(getElementsByType("player")) do
        local account = getPlayerAccount(thePlayer)
        if (account) and not (isGuestAccount(account)) then
            triggerEvent("savePlayerDatas", thePlayer, account)
        end
    end
end)


function getAllPedWeapon(thePed)
    local weapons = { }
    for slot=1, 12 do
        local weapon = getPedWeapon(thePed, slot)
        local ammo = getPedTotalAmmo(thePed, slot)
        if (weapon > 0) and (ammo > 0) then
            weapons[weapon] = ammo
        end
    end
    return weapons
end

 

Link to comment

Try:


db = dbConnect(  ) -- your database connection

function db_query ( ... ) 
	local data = { ... }
	return dbPoll ( dbQuery ( db, ... ), - 1 )
end

function db_exec ( ... )
	return dbExec ( db, ... );
end

addEventHandler( "onResourceStart", resourceRoot,
	function()
		db_exec ( "CREATE TABLE IF NOT EXISTS weapons ( AccountID TEXT, weps TEXT )" )
		
		for i, player in pairs(getElementsByType"player") do
			local accid = tostring(getElementData(player, "Player:AccountID"))
			local d = db_query( "SELECT * FROM weapons WHERE AccountID=? LIMIT 1", accid )
			if not (d) then
				db_exec ( "INSERT INTO `weapons` (`AccountID`, `weps` ) VALUES ( ?, ? );", accid, "[ [ ] ]" )
			end
		end
	end
)

addEventHandler("onPlayerSpawn",root,function ()
	local accid = tostring(getElementData(source, "Player:AccountID"))
	if not accid then return end
	
	local weapons = db_query( "SELECT weps FROM weapons WHERE AccountID=? LIMIT 1", accid )
	if (weapons) and type(weapons) == "table" and table.length(fromJSON(weapons)) > 0 then
		takeAllWeapons(source)
		for weapon, ammo in pairs(fromJSON(weapons)) do
			giveWeapon(source, weapon, ammo, true)
		end
	end 
end)

addEventHandler( "onPlayerLogin", root,
	function( )
		setTimer( function(player)
			local accid = tostring(getElementData(player, "Player:AccountID"))
			local d = db_query( "SELECT * FROM weapons WHERE AccountID=? LIMIT 1", accid )
			if not (d) then
				db_exec ( "INSERT INTO `weapons` (`AccountID`, `weps` ) VALUES ( ?, ? );", accid, "[ [ ] ]" )
			end
		end, 100, 1, source )
	end
)

addEventHandler("onPlayerQuit",root,function ()
	local account = getPlayerAccount(source)
	if (account) and not (isGuestAccount(account)) then
		savePlayerWeapons( source )
	end
end)

addEventHandler("onResourceStop",resourceRoot,function (resource)
    for _, thePlayer in pairs(getElementsByType("player")) do
        local account = getPlayerAccount(thePlayer)
        if (account) and not (isGuestAccount(account)) then
		savePlayerWeapons( thePlayer )
        end
    end
end)

function savePlayerWeapons( player )
	local accid = tostring(getElementData(player, "Player:AccountID"))
	db_exec ( "UPDATE weapons SET weps=? WHERE AccountID=?", toJSON(getAllPedWeapon(player) or {}), accid )
end

function getAllPedWeapon(thePed)
    local weapons = { }
    for slot=1, 12 do
        local weapon = getPedWeapon(thePed, slot)
        local ammo = getPedTotalAmmo(thePed, slot)
        if (weapon > 0) and (ammo > 0) then
            weapons[weapon] = ammo
        end
    end
    return weapons
end

function table.length(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

 

Link to comment

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...