Jump to content

Saving data problem


Chlorek

Recommended Posts

Posted

Hi people. I made userpanel script with saving car mods, and it (I don't know why) doesn't work :? I don't know does not work saving or loading mods list. My source:

function savecar1() 
    local car2s = getElementData(source, "car1D") 
        local upgrades1 = getVehicleUpgrades(car2s) --maybe problem is here? 
    local color1, color2, color3, color4 = getVehicleColor ( car2s ) 
    ------------------------------------------------------------------------- 
    setAccountData (getPlayerAccount (source), "clroleplay-upgr1", upgrades1) 
    setAccountData (getPlayerAccount (source), "clroleplay-col1", color1) 
    setAccountData (getPlayerAccount (source), "clroleplay-col2", color2) 
    setAccountData (getPlayerAccount (source), "clroleplay-col3", color3) 
    setAccountData (getPlayerAccount (source), "clroleplay-col4", color4) 
    ------------------------------------------------------------------------- 
    outputChatBox("Vehicle #1 Saved!", source, 255, 255, 0) 
end 
addEventHandler ("savecar1", getRootElement(), savecar1) 
  

And my loading function:

function spawncar1() 
    local car2destroy = getElementData(source, "car1D") 
    destroyElement (car2destroy) 
    local x,y,z = getElementPosition(source) 
    x = x + 5 
    local carID1 = getAccountData (getPlayerAccount (source), "clroleplay-car1ID") 
    if(tostring(carID1) == "false" or tostring(carID1) == "" or tostring(carID1) == nil)then 
         outputChatBox("This slot is empty!", source, 255, 255, 0) 
    else 
    local car1 = createVehicle(tonumber(carID1),x,y,z) 
    setElementData (source, "car1D", car1) 
    local color1 = getAccountData (getPlayerAccount (source), "clroleplay-col1") 
    local color2 = getAccountData (getPlayerAccount (source), "clroleplay-col2") 
    local color3 = getAccountData (getPlayerAccount (source), "clroleplay-col3") 
    local color4 = getAccountData (getPlayerAccount (source), "clroleplay-col4") 
    local upgrades1 = nil 
    local upgrades1 = {} 
    local upgrades1 = getAccountData (getPlayerAccount (source), "clroleplay-upgr1") 
    local car2save = getElementData(source, "car1D") 
    setVehicleColor( car1, color1, color2, color3, color4 ) 
    for i,v in ipairs (upgrades1) do 
        addVehicleUpgrade (car1, v) 
    end 
    end 
end 
addEventHandler ("spawncar1", getRootElement(), spawncar1) 

I think it's simple error but I can't find a way to fix that. It's one of the oldest my scripts but I never found answer what's wrong :(

Posted

Prefer? Most of the advanced scripters prefer SQL to account data (not me).

I use accounts because I always thought it's easier than SQL (and still think so).

Posted

Yea, I saw a lot of scripts with use SQL but it's not the easiest method. I'll use that but just I want to know is there any other, safe method.

Posted

It isn't, man. Just some more lines. Much work means about 4 hours working for one part of a whole gamemode, that's much work. This? Nope.

Posted

Some more lines... hmmm, if I want to save that in SQL db it's same work. I'll make both methods then I'll check what's simpler and how it works.

Posted

Try https://wiki.multitheftauto.com/wiki/ToJSON / fromJSON.

You can use it to convert the table into a string and store it.

Afterwards, load the string and use fromJSON to convert it back to a table.

function savecar1() 
    local car2s = getElementData(source, "car1D") 
        local upgrades1 = toJSON(getVehicleUpgrades(car2s)) 
    local color1, color2, color3, color4 = getVehicleColor ( car2s ) 
    ------------------------------------------------------------------------- 
    setAccountData (getPlayerAccount (source), "clroleplay-upgr1", upgrades1) 
    setAccountData (getPlayerAccount (source), "clroleplay-col1", color1) 
    setAccountData (getPlayerAccount (source), "clroleplay-col2", color2) 
    setAccountData (getPlayerAccount (source), "clroleplay-col3", color3) 
    setAccountData (getPlayerAccount (source), "clroleplay-col4", color4) 
    ------------------------------------------------------------------------- 
    outputChatBox("Vehicle #1 Saved!", source, 255, 255, 0) 
end 
addEventHandler ("savecar1", getRootElement(), savecar1) 
  

function spawncar1() 
    local car2destroy = getElementData(source, "car1D") 
    destroyElement (car2destroy) 
    local x,y,z = getElementPosition(source) 
    x = x + 5 
    local carID1 = getAccountData (getPlayerAccount (source), "clroleplay-car1ID") 
    if(tostring(carID1) == "false" or tostring(carID1) == "" or tostring(carID1) == nil)then 
         outputChatBox("This slot is empty!", source, 255, 255, 0) 
    else 
    local car1 = createVehicle(tonumber(carID1),x,y,z) 
    setElementData (source, "car1D", car1) 
    local color1 = getAccountData (getPlayerAccount (source), "clroleplay-col1") 
    local color2 = getAccountData (getPlayerAccount (source), "clroleplay-col2") 
    local color3 = getAccountData (getPlayerAccount (source), "clroleplay-col3") 
    local color4 = getAccountData (getPlayerAccount (source), "clroleplay-col4") 
    --local upgrades1 = nil   -- these two lines aren't needed 
    --local upgrades1 = {} 
    local upgrades1 = fromJSON(getAccountData (getPlayerAccount (source), "clroleplay-upgr1")) 
    local car2save = getElementData(source, "car1D") 
    setVehicleColor( car1, color1, color2, color3, color4 ) 
    for i,v in ipairs (upgrades1) do 
        addVehicleUpgrade (car1, v) 
    end 
    end 
end 
addEventHandler ("spawncar1", getRootElement(), spawncar1) 

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