Jump to content

tables


Drakath

Recommended Posts

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?

Link to comment
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 

Link to comment
  • Moderators
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:

Link to comment
  • Moderators

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.

Link to comment

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 ]

Link to comment

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.

Link to comment

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 
) 

Link to comment

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 
) 

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