Jump to content
  • 0

Random lag on save


XeniusORIGINAL

Question

Hello. I have two script, and if number of players is over 150 then this causes random lag.

1. Inventory Save:

function saveItems(element, byquit) 
    if getElementType(element) == "vehicle" and getVehicleType(element) == "BMX" then return end 
    local dbid = tonumber(getElementData(element,"dbid")) or 0 
    if dbid > 0 then 
        for i=0,31 do 
            local item = tonumber(getElementData(element,"items"..i)) or 0 
            if item > 0 then 
                local dutyItem = tonumber(getElementData(element,"dutyitem"..i..item)) or 0 
                if dutyItem ~= 1 then 
                    local itemValue = tonumber(getElementData(element,"itemvalue"..i..item)) or 0 
                    local id = tonumber(getElementData(element,"itemid"..i..item)) or 0 
                    local _, lastid = mysql:query_free("INSERT INTO items (id,itemValue,itemID,type,owner,slot,`index`) VALUES ('"..id.."','"..itemValue.."','"..item.."','"..getType(element).."','"..dbid.."','"..i.."','0') ON DUPLICATE KEY UPDATE id=VALUES(id),itemValue=VALUES(itemValue),itemID=VALUES(itemID),type=VALUES(type),owner=VALUES(owner),slot=VALUES(slot),`index`=VALUES(`index`)", true) 
                    if id == 0 and not byquit then 
                        if isElement(element) then 
                            setElementData(element,"itemid"..i..item, lastid) 
                        end 
                    end 
                end 
            end 
        end 
    end 
end 
addEvent("saveItems", true) 
addEventHandler("saveItems", getRootElement(), saveItems) 

And:

function quitPlayer() 
    saveItems(source, true) 
end 
addEventHandler ("onPlayerQuit", getRootElement(), quitPlayer ) 
  
addEventHandler("onResourceStop", resourceRoot, function() 
    for k,v in ipairs(getElementsByType("player")) do 
        if getElementData(v, "spawned") then 
            saveItems(v) 
        end 
    end 
    for k1,v1 in ipairs(getElementsByType("vehicle")) do 
        saveItems(v1) 
    end 
    for k2,v2 in ipairs(getElementsByType("object", getResourceRootElement(getResourceFromName("ex_inventory")))) do 
        saveItems(v2) 
    end 
end) 

2. Account save:

addEventHandler ( "onPlayerQuit", getRootElement(), function() 
    if getElementData(source, "spawned") then 
        savePlayer(source) 
    end 
end ) 
  
function savePlayer(ps) 
    if isElement(ps) then 
        local id = tonumber(getElementData(ps, "dbid")) or 0 
        if id > 0 then 
            local x, y, z = getElementPosition(ps) 
            local rot = getPedRotation(ps) 
            local interior = getElementInterior(ps) 
            local dimension = getElementDimension(ps) 
            local faction = tonumber(getElementData(ps, "char:Faction")) or 0 
            local factionrank = tonumber(getElementData(ps, "char:FactionRank")) or 1 
            local factionleader = tonumber(getElementData(ps, "char:FactionLeader")) or 0 
            local dutyskin = tonumber(getElementData(ps, "char:DutySkin")) or 0 
            local dead = tonumber(getElementData(ps, "dead")) or 0 
            local skin = tonumber(getElementData(ps, "char:skin")) or 0 
            local hlp = tonumber(getElementData(ps, "char:HLP")) or 0 
            local pr = tonumber(getElementData(ps, "char:pR")) or 0 
            local pg = tonumber(getElementData(ps, "char:pG")) or 0 
            local pb = tonumber(getElementData(ps, "char:pB")) or 0 
            local job = tonumber(getElementData(ps, "char:job")) or 0 
            local timespenton = tonumber(getElementData(ps, "char:timeSpent")) or 0 
            local bankmoney = tonumber(getElementData(ps, "char:Bankmoney")) or 0 
            local jailreason = tostring(getElementData(ps, "char:jailReason")) or "Nincs" 
            local adminnick = tostring(getElementData(ps, "user:adminnick")) or "Ismeretlen" 
            local charname = tostring(getElementData(ps, "char:Name")) or "Ismeretlen" 
            local jailtime = tonumber(getElementData(ps, "char:jailTime")) or 0 
            local money = tonumber(getElementData(ps, "char:Money")) or 0 
            local ehseg = tonumber(getElementData(ps, "char:Hunger")) or 0 
            local radio = tonumber(getElementData(ps, "char:radioChannel")) or 0 
            local admin = tonumber(getElementData(ps, "user:adminlevel")) or 0 
            local accid = tonumber(getElementData(ps, "char:gameaccountid")) or 0 
            local license = getElementData(ps, "char:drivingLicense") 
            if license then 
                license = 1 
            else 
                license = 0 
            end 
  
            mysql:query_free("UPDATE karakterek SET rotation = '"..rot.."',dead = '"..dead.."',hlp = '"..hlp.."',jailreason = '"..jailreason.."',dutyskin = '"..dutyskin.."',factionrank = '"..factionrank.."',factionleader = '"..factionleader.."',faction = '"..faction.."',pr = '"..pr.."',pg = '"..pg.."',pb = '"..pb.."', bankmoney = '"..bankmoney.."', adminnick = '"..adminnick.."', job = '"..job.."', timeSpentOn = '"..timespenton.."', jailTime = '"..jailtime.."', skin = '".. skin .."', charactername = '"..charname.."', money = '"..money.."', ehseg = '"..ehseg.."', thirsty = '"..tonumber(getElementData(ps, "char:Thirsty")).."', health = '"..getElementHealth(ps).."', armor = '"..getPedArmor(ps).."', x = '"..x.."', y = '"..y.."', z = '"..z.."', radiochannel = '"..radio.."', car_license = '"..license.."', interior_id = '"..interior.."', dimension_id = '"..dimension.."' WHERE id = '"..id.."'") 
            mysql:query_free("UPDATE account SET admin = '"..admin.."' WHERE id = '"..accid.."'") 
        end 
    end 
end 
addEvent("savePlayer", true) 
addEventHandler("savePlayer", getRootElement(), savePlayer) 

And the mysql function:

function query_free(q,poll) 
    local this = #results + 1 
    results[this] = dbQuery(dbHandler, q) 
    if poll then 
        local result, num_affected_rows, last_insert_id = dbPoll(results[this], -1) 
        if result == nil then 
            dbFree(results[this]) 
            return this, nil 
        elseif result == false then 
            dbFree(results[this]) 
            return this, nil 
        else 
            dbFree(results[this]) 
            return this, tonumber(last_insert_id) 
        end 
    end 
    dbFree(results[this]) 
    return this 
end 

q1dKlIV.png

I don't know how to fix this, no more idea.

if anyone can help, then please reply.

Link to comment

2 answers to this question

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