Jump to content

tables


Drakath

Recommended Posts

Posted
function yo () 
          local u = nil 
          local u = {} 
          local u = getAccountData (getPlayerAccount(source), "cu") 
          for i,v in ipairs (u) do 
            addVehicleUpgrade (vehicle, v) 
          end 
        end 
addCommandHandler("upgrade", yo)  

ERROR: script\server.lua:5: bad argument #1 to 'ipairs' (table expected, got number)

How can I fix it?

Posted
function yo () 
 local u  = getElementsByType ( "player" )  -- get all players 
          for i,v in ipairs (u) do 
if getAccountData ( getPlayerAccount (v), "cu" ) == true then -- you can replace this true with whatever you used in setAccount Data but remember it should be between "" for example, "YOURTEXT" 
 local theVehicle = getPedOccupiedVehicle ( v )  
            addVehicleUpgrade (theVehicle, 1000) -- upgrade value "1000" 
              end 
        end 
end 
addCommandHandler("upgrade", yo) 

Well for tables you should use

table.insert 

  • Moderators
Posted
function yo () 
 local u  = getElementsByType ( "player" )  -- get all players 
          for i,v in ipairs (u) do 
if getAccountData ( getPlayerAccount (v), "cu" ) == true then -- you can replace this true with whatever you used in setAccount Data but remember it should be between "" for example, "YOURTEXT" 
 local theVehicle = getPedOccupiedVehicle ( v )  
            addVehicleUpgrade (theVehicle, 1000) -- upgrade value "1000" 
              end 
        end 
end 
addCommandHandler("upgrade", yo) 

Well for tables you should use

table.insert 

Don't try to help him when you don't know what he is talking about. :idea:

Posted

I'm not sure how does this JSON thing works. Would it work like that:

local json = toJSON ( { getAccountData (getPlayerAccount(source), "cu") } ) 

?

  • Moderators
Posted

toJSON is before you save it.

local myTable = {} 
setAccountData (getPlayerAccount(source), "cu",toJSON (myTable)) 
  

fromJSON is when you load it.

local data = getAccountData (getPlayerAccount(source), "cu") 
if data then 
    local u = fromJSON ( data ) 
  
end 

and no I have never used this(JSON) before.

Posted

Alright, last question. If I use table.insert to upgrade wheels and then upgrade the wheels again but with a different upgrade, will it replace the old one or will they both stay? If they will both stay then how can I remove a particular upgrade?

Posted

Oh, one last, last thing.

I downloaded modshop resource form community and I modified one part to setAccountData for the vehicle.

addEvent( "modShop_playerLeaveModShop", true ) 
addEventHandler( "modShop_playerLeaveModShop", getRootElement( ), 
    function( vehicle, itemsCost, upgrades, colors, paintjob, shopName ) 
        local pMoney = getPlayerMoney( source ) 
        if pMoney >= itemsCost then 
            modTheVehicle( vehicle, upgrades, colors, paintjob, shopName ) 
            takePlayerMoney( source, itemsCost ) 
            triggerClientEvent( source, "modShop_moddingConfirmed", source ) 
            local upgs = unpack(upgrades) 
            local account = getPlayerAccount(source) 
            local myTable = {} 
        if account then  
            table.insert ( myTable, upgs ) 
            setAccountData (account, "cu",toJSON (myTable)) 
        else 
            outputChatBox( "#FF0000Inufficient founds! #00FF00Your pocket shows $"..tostring( getPlayerMoney( source ) )..".#FFFFFF Uninstall some upgrades.", source, 0,0,0,true) 
        end 
    end 
end 
) 

But it kind of messes up the account data and end up with bad argument 'addVehicleUpgrade'. I tried unpacking the table to insert the ids, but it didn't work. My account data was something like this: [ [ "[ [ \"[ [ \\\"[ [ \\\\\\\"[ [ \\\\\\\\\\\\\\\"[ [ 1008 ] ]\\\\\\\\\\\\\\\", 1008 ] ]\\\\\\\", 1010 ] ]\\\", [ 1087, 1073 ]

Posted

Account data can save up to 128 characters, if you have more than 128, only 128 will be saved and the rest will be lost therefore your table won't work then so you better get another way to save the string such as SQL.

Posted

I don't want to make account data for every vehicle upgrade because it would spam the database. Anyway the problem is that it doesn't save any upgrades. It works if I put: table.insert ( myTable, 1010 ) but not when I put: table.insert ( myTable, upgs )

which means something is wrong with upgs.

  • Moderators
Posted

unpack does this:

  
local data1,data2,data3 = unpack({"A","B","C"}) 
outputChatbox(data1 .. " " .. data2 .. " " .. data3) --  A B C 

Posted

You don't need unpack, IIYAMA explained what it does.

Why you even trying to unpack it, you can save upgrades table as JSON string directly.

Posted

I had upgrade 1010. I upgraded two items: hydraulics and wheels. However only the first item (hydraulics) have been saved. Also I have lost upgrade 1010.

addEvent( "modShop_playerLeaveModShop", true ) 
addEventHandler( "modShop_playerLeaveModShop", getRootElement( ), 
    function( vehicle, itemsCost, upgrades, colors, paintjob, shopName ) 
        local pMoney = getPlayerMoney( source ) 
        local account = getPlayerAccount(source) 
        if pMoney >= itemsCost and account then 
            modTheVehicle( vehicle, upgrades, colors, paintjob, shopName ) 
            takePlayerMoney( source, itemsCost ) 
            triggerClientEvent( source, "modShop_moddingConfirmed", source ) 
            local data = getAccountData (getPlayerAccount(source), "cu") 
            local myTable = {} 
            table.insert ( myTable, data ) 
            table.insert ( myTable, upgrades ) 
            setAccountData (account, "cu",toJSON (myTable)) 
            outputChatBox( "Upgrades: "..upgrades, source, 0,0,0,true) 
        else 
            outputChatBox( "#FF0000Inufficient founds! #00FF00Your pocket shows $"..tostring( getPlayerMoney( source ) )..".#FFFFFF Uninstall some upgrades.", source, 0,0,0,true) 
    end 
end 
) 

Posted

Try this:

addEvent( "modShop_playerLeaveModShop", true ) 
addEventHandler( "modShop_playerLeaveModShop", getRootElement( ), 
    function( vehicle, itemsCost, upgrades, colors, paintjob, shopName ) 
        local pMoney = getPlayerMoney( source ) 
        local account = getPlayerAccount(source) 
        if pMoney >= itemsCost and account then 
            modTheVehicle( vehicle, upgrades, colors, paintjob, shopName ) 
            takePlayerMoney( source, itemsCost ) 
            triggerClientEvent( source, "modShop_moddingConfirmed", source ) 
            local myTable = {} 
            for i, ups in ipairs (upgrades) do 
                table.insert ( myTable, ups ) 
            end 
            setAccountData (account, "cu",toJSON (myTable)) 
        else 
            outputChatBox( "#FF0000Inufficient founds! #00FF00Your pocket shows $"..tostring( getPlayerMoney( source ) )..".#FFFFFF Uninstall some upgrades.", source, 0,0,0,true) 
    end 
end 
) 

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