Drakath Posted February 1, 2014 Share Posted February 1, 2014 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
Moderators IIYAMA Posted February 1, 2014 Moderators Share Posted February 1, 2014 u isn't a table. getAccountData (getPlayerAccount(source), "cu") --< convert first to tojson: https://wiki.multitheftauto.com/wiki/ToJSON -- save setAccountData -- load getAccountData https://wiki.multitheftauto.com/wiki/FromJSON convert fromJSON Link to comment
Saml1er Posted February 1, 2014 Share Posted February 1, 2014 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 IIYAMA Posted February 1, 2014 Moderators Share Posted February 1, 2014 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. Link to comment
Drakath Posted February 1, 2014 Author Share Posted February 1, 2014 I'm not sure how does this JSON thing works. Would it work like that: local json = toJSON ( { getAccountData (getPlayerAccount(source), "cu") } ) ? Link to comment
Moderators IIYAMA Posted February 1, 2014 Moderators Share Posted February 1, 2014 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
Castillo Posted February 1, 2014 Share Posted February 1, 2014 Remember that account data got a string limit, means that it can end cutting your JSON string. Link to comment
Drakath Posted February 1, 2014 Author Share Posted February 1, 2014 Well, I got this error: bad argument #1 to 'ipairs' (table expected, got string) Link to comment
Drakath Posted February 1, 2014 Author Share Posted February 1, 2014 (edited) ****** This is how I got my data: ***** Edited February 1, 2014 by Guest Link to comment
Castillo Posted February 1, 2014 Share Posted February 1, 2014 local myTable = {} local myTable = 1010 You are overwritting the 'myTable' variable with a number. Link to comment
Drakath Posted February 1, 2014 Author Share Posted February 1, 2014 So how do I set nitro for that data? Link to comment
Castillo Posted February 1, 2014 Share Posted February 1, 2014 Well, you can do something like this: local myTable = { } myTable [ 1 ] = 1010 Or you can use table.insert: table.insert ( myTable, 1010 ) Link to comment
Drakath Posted February 1, 2014 Author Share Posted February 1, 2014 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? Link to comment
Castillo Posted February 1, 2014 Share Posted February 1, 2014 Well, if you are going to use that table format, then you must loop the table to remove the other upgrade. Link to comment
Drakath Posted February 1, 2014 Author Share Posted February 1, 2014 Should I use table.remove to do that? Link to comment
Castillo Posted February 1, 2014 Share Posted February 1, 2014 Yes, but table.remove needs the index of the item you want to remove. Link to comment
Drakath Posted February 2, 2014 Author Share Posted February 2, 2014 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
TAPL Posted February 2, 2014 Share Posted February 2, 2014 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. Link to comment
Drakath Posted February 2, 2014 Author Share Posted February 2, 2014 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
Moderators IIYAMA Posted February 2, 2014 Moderators Share Posted February 2, 2014 unpack does this: local data1,data2,data3 = unpack({"A","B","C"}) outputChatbox(data1 .. " " .. data2 .. " " .. data3) -- A B C Link to comment
Drakath Posted February 2, 2014 Author Share Posted February 2, 2014 I only need to unpack one table which is: 'upgrades'. Link to comment
TAPL Posted February 2, 2014 Share Posted February 2, 2014 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. Link to comment
Drakath Posted February 2, 2014 Author Share Posted February 2, 2014 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
TAPL Posted February 2, 2014 Share Posted February 2, 2014 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
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 accountSign in
Already have an account? Sign in here.
Sign In Now