Jump to content

Saving player statistics failure


Recommended Posts

When a player disconnects, I'd like to save all their statistics, but so far the database isn't getting updated at all, what is incorrect in my coding? Nothing shows up in the debug either:

I let you know that I had a for loop on the player element, but that caused other issues.

  
local mysql=exports.mysql 
function savePlayer(reason) 
    outputChatBox( tostring(source) )   
    local vehicle = getPedOccupiedVehicle(source) 
        if (vehicle) then 
            local seat = getPedOccupiedVehicleSeat(source) 
            triggerEvent("onVehicleExit", vehicle, source, seat) 
        end 
         
        local x, y, z, rot, health, armour, interior, dimension 
         
        local x, y, z = getElementPosition(source) 
        local rot = getPedRotation(source) 
        local health = getElementHealth(source) 
        local armor = getPedArmor(source) 
        local interior = getElementInterior(source) 
        local dimension = getElementDimension(source) 
        --money = getElementData(source, "money") 
         
        local skin = getElementModel(source) 
         
        mysql:query_free("UPDATE characters SET x='" .. mysql:escape_string(x) .. "', y='" .. mysql:escape_string(y) .. "', z='" .. mysql:escape_string(z) .. "', rotation='" .. mysql:escape_string(rot) .. "', health='" .. mysql:escape_string(health) .. "', armor='" .. mysql:escape_string(armor) .. "', dimension_id='" .. mysql:escape_string(dimension) .. "', interior_id='" .. mysql:escape_string(interior) .. "' WHERE id="..(mysql:escape_string(tonumber(getElementData(source, "character:id"))))) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), savePlayer) 
addEvent("savePlayer", false) 
addEventHandler("savePlayer", getRootElement(), savePlayer) 
addCommandHandler("saveall", savePlayer) 
  

Link to comment

i mean, for example, if you want to save the dimension of the player ....

  
addEventHandler ("onPlayerQuit", getRootElement (), 
   function ()  
        local account = getPlayerAccount (source); 
        setAccountData (account, "dimension", getElementDimension(source));  
   end);  
  

when player connects again to the server, you can do this to restore the dimension in which the player is in.

  
addEventHandler ("onPlayerJoin", getRootElement(),  
    function ()  
         local account = getPlayerAccount(source); 
         local dimension = getAccountData(accout,"dimension"); 
         if dimension then setElementDimension (source, tonumber(dimension); end;  
    end);  
  

is just another way to do it.

Link to comment
i mean, for example, if you want to save the dimension of the player ....
  
addEventHandler ("onPlayerQuit", getRootElement (), 
   function ()  
        local account = getPlayerAccount (source); 
        setAccountData (account, "dimension", getElementDimension(source));  
   end);  
  

when player connects again to the server, you can do this to restore the dimension in which the player is in.

  
addEventHandler ("onPlayerJoin", getRootElement(),  
    function ()  
         local account = getPlayerAccount(source); 
         local dimension = getAccountData(accout,"dimension"); 
         if dimension then setElementDimension (source, tonumber(dimension); end;  
    end);  
  

is just another way to do it.

Actually it's impossible to collect data without knowing if the players logged in or not, hence no account defined.

Use "onPlayerLogin"

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