Tahseen Posted December 26, 2016 Share Posted December 26, 2016 (edited) Hello guys,i need you to help me in my script which doesn't work and if it is working it usually saves 1 vehicle only and i don't want it to work like that so if you have knowledge of fixing it then please help me. Here is the code: createdVehicle = {} function buyVehicle(player,id,price) local money = getPlayerMoney(player) if ( money >= price ) and ( not ( isGuestAccount(getPlayerAccount(player)) ) ) and ( not ( getElementData(player,"VS.Count") == 5 ) ) then local x, y, z = getElementPosition(player) if ( isElement(createdVehicle[player]) ) then destroyElement(createdVehicle[player]) end createdVehicle[player] = createVehicle(id,x,y-3,z) if ( createdVehicle[player] ) then if ( getElementData(player,"VS.Vehicles") == false ) then local aTable = {} local jsone = toJSON(aTable) setElementData(player,"VS.Vehicles",jsone) local i = getElementData(player,"VS.Count") if ( i == false ) then setElementData(player,"VS.Count",0) else setElementData(player,"VS.Count",tonumber(i)+1) end end local theTable = {fromJSON(getElementData(player,"VS.Vehicles"))} if ( type(theTable) == "table" ) and ( not ( theTable == false ) ) or ( not ( theTable == nil ) ) then for ind,ent in ipairs(theTable) do local i = getElementData(player,"VS.Count")+1 theTable[i][1] = id --[[ HERE is the problem ]]-- theTable[i][2] = 1000 --[[ and here ]]-- theTable[i][3] = price/2 --[[ and here ]]-- setElementData(player,"VS.Vehicles",toJSON(theTable)) setElementData(player,"VS.Count",i) setElementData(createdVehicle[player],"Owner",player) setElementData(createdVehicle[player],"VS.Count",i) takePlayerMoney(player,price) outputChatBox("[Vehicle System] You have bought ".. getVehicleNameFromModel(id) .." for ".. price .."$ !",player,0,255,0) end else outputChatBox("[Vehicle System] Error purchasing a car!",player,255,0,0) end end elseif ( isGuestAccount(getPlayerAccount(player)) ) then outputChatBox("[Vehicle System] Please login in order to buy a vehicle!",player,255,0,0) elseif ( money < price ) then outputChatBox("[Vehicle System] You don't have enough money to buy this car!",player,255,0,0) elseif ( getElementData(player,"VS.Count") == 5 ) then outputChatBox("[Vehicle System] You have reached the maximum amount of vehicles to purchase!",255,0,0) end end Thanks in advance. Edited December 26, 2016 by Tahseen Link to comment
aka Blue Posted December 26, 2016 Share Posted December 26, 2016 If you dont show the table or how you save that "table" we cant help you. Link to comment
Tahseen Posted December 26, 2016 Author Share Posted December 26, 2016 setElementData(player,"VS.Vehicles",toJSON(theTable)) I used this to save the table and to get the table by this: local theTable = {fromJSON(getElementData(player,"VS.Vehicles"))} Btw they are already in the code above. Link to comment
myonlake Posted December 27, 2016 Share Posted December 27, 2016 fromJSON already returns a table. You are wrapping the table with a table, which means the whole structure gets corrupted. Try removing the table wrap { ... } around the fromJSON and you should be fine. Link to comment
Tahseen Posted December 27, 2016 Author Share Posted December 27, 2016 3 minutes ago, mgmmcaobbm said: fromJSON already returns a table. You are wrapping the table with a table, which means the whole structure gets corrupted. Try removing the table wrap { ... } around the fromJSON and you should be fine. Already doing it,thanks for support! Link to comment
myonlake Posted December 27, 2016 Share Posted December 27, 2016 Just now, Tahseen said: Already doing it,thanks for support! You're welcome. 1 Link to comment
Tahseen Posted December 27, 2016 Author Share Posted December 27, 2016 24 minutes ago, mgmmcaobbm said: fromJSON already returns a table. You are wrapping the table with a table, which means the whole structure gets corrupted. Try removing the table wrap { ... } around the fromJSON and you should be fine. But how can i catch the values in the table? Because they aren't responding. Link to comment
myonlake Posted December 27, 2016 Share Posted December 27, 2016 (edited) You haven't initialized the index in the table, that's why it cannot set the values. You also had a pretty dangerous for loop over that which can potentially crash your server. No idea why you wanted to iterate through the vehicles. For both of the versions, I fixed the last outputChatBox that had no target player defined in the arguments, it's now set to player like the other ones. Here's your script with minimal changes: createdVehicle = {} function buyVehicle(player,id,price) local money = getPlayerMoney(player) if ( money >= price ) and ( not ( isGuestAccount(getPlayerAccount(player)) ) ) and ( not ( getElementData(player,"VS.Count") == 5 ) ) then local x, y, z = getElementPosition(player) if ( isElement(createdVehicle[player]) ) then destroyElement(createdVehicle[player]) end createdVehicle[player] = createVehicle(id,x,y-3,z) if ( createdVehicle[player] ) then if ( getElementData(player,"VS.Vehicles") == false ) then local aTable = {} local jsone = toJSON(aTable) setElementData(player,"VS.Vehicles",jsone) local i = getElementData(player,"VS.Count") if ( i == false ) then setElementData(player,"VS.Count",0) else setElementData(player,"VS.Count",tonumber(i)+1) end end local theTable = {fromJSON(getElementData(player,"VS.Vehicles"))} if ( type(theTable) == "table" ) and ( not ( theTable == false ) ) or ( not ( theTable == nil ) ) then local i = getElementData(player,"VS.Count")+1 theTable[i]={} theTable[i][1] = id --[[ HERE is the problem ]]-- theTable[i][2] = 1000 --[[ and here ]]-- theTable[i][3] = price/2 --[[ and here ]]-- setElementData(player,"VS.Vehicles",toJSON(theTable)) setElementData(player,"VS.Count",i) setElementData(createdVehicle[player],"Owner",player) setElementData(createdVehicle[player],"VS.Count",i) takePlayerMoney(player,price) outputChatBox("[Vehicle System] You have bought ".. getVehicleNameFromModel(id) .." for ".. price .."$ !",player,0,255,0) else outputChatBox("[Vehicle System] Error purchasing a car!",player,255,0,0) end end elseif ( isGuestAccount(getPlayerAccount(player)) ) then outputChatBox("[Vehicle System] Please login in order to buy a vehicle!",player,255,0,0) elseif ( money < price ) then outputChatBox("[Vehicle System] You don't have enough money to buy this car!",player,255,0,0) elseif ( getElementData(player,"VS.Count") == 5 ) then outputChatBox("[Vehicle System] You have reached the maximum amount of vehicles to purchase!",player,255,0,0) end end A little bit more cleaned up, if you want it: createdVehicle = { } function buyVehicle( player, id, price ) local money = getPlayerMoney( player ) if ( money >= price ) and ( not isGuestAccount( getPlayerAccount( player ) ) ) and ( not ( getElementData( player, "VS.Count" ) == 5 ) ) then if ( isElement( createdVehicle[ player ] ) ) then destroyElement( createdVehicle[ player ] ) end local x, y, z = getElementPosition( player ) local _, _, rz = getElementRotation( player ) createdVehicle[ player ] = createVehicle( id, Matrix( Vector3( x, y, z ), Vector3( 0, 0, rz ) ):transformPosition( Vector3( 3, 0, 1 ) ), 0, 0, rz ) if ( isElement( createdVehicle[ player ] ) ) then local vehicles = getElementData( player, "VS.Vehicles" ) if ( type( vehicles ) ~= 'table' ) then vehicles = { } setElementData( player, "VS.Vehicles", vehicles ) setElementData( player, "VS.Count", 0 ) end local index = getElementData( player, "VS.Count" ) + 1 vehicles[ index ] = { } vehicles[ index ].id = id vehicles[ index ].something = 1000 vehicles[ index ].price = price / 2 setElementData( player, "VS.Vehicles", vehicles ) setElementData( player, "VS.Count", index ) setElementData( createdVehicle[ player ], "Owner", player ) setElementData( createdVehicle[ player ], "VS.Count", index ) takePlayerMoney( player, price ) outputChatBox( "[Vehicle System] You have bought " .. getVehicleNameFromModel( id ) .. " for $" .. price .. "!", player, 0, 255, 0 ) end elseif ( isGuestAccount( getPlayerAccount( player ) ) ) then outputChatBox( "[Vehicle System] Please login in order to buy a vehicle!", player, 255, 0, 0 ) elseif ( money < price ) then outputChatBox( "[Vehicle System] You don't have enough money to buy this car!", player, 255, 0, 0 ) elseif ( getElementData( player, "VS.Count" ) == 5 ) then outputChatBox( "[Vehicle System] You have reached the maximum amount of vehicles to purchase!", player, 255, 0, 0 ) end end Edited December 27, 2016 by mgmmcaobbm 1 Link to comment
Tahseen Posted December 27, 2016 Author Share Posted December 27, 2016 6 minutes ago, mgmmcaobbm said: You haven't initialized the index in the table, that's why it cannot set the values. Here's your script with minimal changes: createdVehicle = {} function buyVehicle(player,id,price) local money = getPlayerMoney(player) if ( money >= price ) and ( not ( isGuestAccount(getPlayerAccount(player)) ) ) and ( not ( getElementData(player,"VS.Count") == 5 ) ) then local x, y, z = getElementPosition(player) if ( isElement(createdVehicle[player]) ) then destroyElement(createdVehicle[player]) end createdVehicle[player] = createVehicle(id,x,y-3,z) if ( createdVehicle[player] ) then if ( getElementData(player,"VS.Vehicles") == false ) then local aTable = {} local jsone = toJSON(aTable) setElementData(player,"VS.Vehicles",jsone) local i = getElementData(player,"VS.Count") if ( i == false ) then setElementData(player,"VS.Count",0) else setElementData(player,"VS.Count",tonumber(i)+1) end end local theTable = {fromJSON(getElementData(player,"VS.Vehicles"))} if ( type(theTable) == "table" ) and ( not ( theTable == false ) ) or ( not ( theTable == nil ) ) then local i = getElementData(player,"VS.Count")+1 theTable[i]={} theTable[i][1] = id --[[ HERE is the problem ]]-- theTable[i][2] = 1000 --[[ and here ]]-- theTable[i][3] = price/2 --[[ and here ]]-- setElementData(player,"VS.Vehicles",toJSON(theTable)) setElementData(player,"VS.Count",i) setElementData(createdVehicle[player],"Owner",player) setElementData(createdVehicle[player],"VS.Count",i) takePlayerMoney(player,price) outputChatBox("[Vehicle System] You have bought ".. getVehicleNameFromModel(id) .." for ".. price .."$ !",player,0,255,0) else outputChatBox("[Vehicle System] Error purchasing a car!",player,255,0,0) end end elseif ( isGuestAccount(getPlayerAccount(player)) ) then outputChatBox("[Vehicle System] Please login in order to buy a vehicle!",player,255,0,0) elseif ( money < price ) then outputChatBox("[Vehicle System] You don't have enough money to buy this car!",player,255,0,0) elseif ( getElementData(player,"VS.Count") == 5 ) then outputChatBox("[Vehicle System] You have reached the maximum amount of vehicles to purchase!",player,255,0,0) end end A little bit more cleaned up, if you want it: createdVehicle = { }function buyVehicle( player, id, price ) local money = getPlayerMoney( player ) if ( money >= price ) and ( not isGuestAccount( getPlayerAccount( player ) ) ) and ( not ( getElementData( player, "VS.Count" ) == 5 ) ) then if ( isElement( createdVehicle[ player ] ) ) then destroyElement( createdVehicle[ player ] ) end local x, y, z = getElementPosition( player ) local _, _, rz = getElementRotation( player ) createdVehicle[ player ] = createVehicle( id, Matrix( Vector3( x, y, z ), Vector3( 0, 0, rz ) ):transformPosition( Vector3( 3, 0, 1 ) ), 0, 0, rz ) if ( createdVehicle[ player ] ) then local vehicles = getElementData( player, "VS.Vehicles" ) if ( type( vehicles ) ~= 'table' ) then vehicles = { } setElementData( player, "VS.Vehicles", vehicles ) setElementData( player, "VS.Count", 0 ) end local index = getElementData( player, "VS.Count" ) + 1 vehicles[ index ] = { } vehicles[ index ].id = id vehicles[ index ].something = 1000 vehicles[ index ].price = price / 2 setElementData( player, "VS.Vehicles", vehicles ) setElementData( player, "VS.Count", index ) setElementData( createdVehicle[ player ], "Owner", player ) setElementData( createdVehicle[ player ], "VS.Count", index ) takePlayerMoney( player, price ) outputChatBox( "[Vehicle System] You have bought " .. getVehicleNameFromModel( id ) .. " for $" .. price .. "!", player, 0, 255, 0 ) end elseif ( isGuestAccount( getPlayerAccount( player ) ) ) then outputChatBox( "[Vehicle System] Please login in order to buy a vehicle!", player, 255, 0, 0 ) elseif ( money < price ) then outputChatBox( "[Vehicle System] You don't have enough money to buy this car!", player, 255, 0, 0 ) elseif ( getElementData( player, "VS.Count" ) == 5 ) then outputChatBox( "[Vehicle System] You have reached the maximum amount of vehicles to purchase!", player, 255, 0, 0 ) endend Thank you so much! I will try it now. Link to comment
myonlake Posted December 27, 2016 Share Posted December 27, 2016 (edited) 7 minutes ago, Tahseen said: Thank you so much! I will try it now. You're welcome. In that cleaned up version I removed the JSON encoding, because in the end, you don't really need it, unless you're outputting or transferring the data somewhere - even then, you should be doing the encoding when you need it, and not just because you can on initialization. Otherwise you're always ending up decoding the data when you need to use it, which is a lot more work than just letting it be. I also made that vehicle spawn properly with the rotation of the player. I also cleaned up the unnecessary keeping of those counts when resetting the vehicles table, since the only case that happens is when you have no vehicles. You can of course change it back if you have some other code which does something that messes up the vehicles data... Edited December 27, 2016 by mgmmcaobbm Link to comment
Tahseen Posted December 27, 2016 Author Share Posted December 27, 2016 5 minutes ago, mgmmcaobbm said: You're welcome. In that cleaned up version I removed the JSON encoding, because in the end, you don't really need it (unless you're outputting or transferring the data somewhere). I also made that vehicle spawn properly with the rotation of the player. I also cleaned up the unnecessary keeping of those counts when resetting the vehicles table, since the only case that happens is when you have no vehicles. You can of course change it back if you have some other code which does something that messes up the vehicles data... And when i want to catch the table in element data what shall i use? local index = getElementData(player,"VS.Count") local theTable = getElementData(player,"VS.Vehicles") for ind,ent in ipairs(theTable) do createVehicle(ent[index].1,1,1,1) end Will this work as an example in client side? Link to comment
myonlake Posted December 27, 2016 Share Posted December 27, 2016 (edited) 10 minutes ago, Tahseen said: And when i want to catch the table in element data what shall i use? local index = getElementData(player,"VS.Count") local theTable = getElementData(player,"VS.Vehicles") for ind,ent in ipairs(theTable) do createVehicle(ent[index].1,1,1,1) end Will this work as an example in client side? You can use those data points client-side. Here is an example /myvehicles which outputs your vehicle data on client-side. addCommandHandler( 'myvehicles', function( ) local vehicles = getElementData( localPlayer, "VS.Vehicles" ) if ( type( vehicles ) == 'table' ) and ( #vehicles > 0 ) then outputChatBox( "Your vehicles:" ) for index, data in ipairs( vehicles ) do outputChatBox( " Vehicle #" .. index .. " is a " .. getVehicleNameFromModel( data.id ) .. " and has a price of $" .. data.price .. "." ) end else outputChatBox( "You have no vehicles.", 255, 0, 0 ) end end ) Tables in Lua: http://lua-users.org/wiki/TablesTutorial For in Lua: http://lua-users.org/wiki/ForTutorial There is also a tutorial on tables here on MTA forums, I see: Edited December 27, 2016 by mgmmcaobbm added tut link 1 Link to comment
Tahseen Posted December 27, 2016 Author Share Posted December 27, 2016 22 minutes ago, mgmmcaobbm said: You can use those data points client-side. Here is an example /myvehicles which outputs your vehicle data on client-side. addCommandHandler( 'myvehicles', function( ) local vehicles = getElementData( localPlayer, "VS.Vehicles" ) if ( type( vehicles ) == 'table' ) and ( #vehicles > 0 ) then outputChatBox( "Your vehicles:" ) for index, data in ipairs( vehicles ) do outputChatBox( " Vehicle #" .. index .. " is a " .. getVehicleNameFromModel( data.id ) .. " and has a price of $" .. data.price .. "." ) end else outputChatBox( "You have no vehicles.", 255, 0, 0 ) end end) Tables in Lua: http://lua-users.org/wiki/TablesTutorial For in Lua: http://lua-users.org/wiki/ForTutorial There is also a tutorial on tables here on MTA forums, I see: Thank you very much! It's now working perfectly! I really appreciate your support to me! Link to comment
myonlake Posted December 27, 2016 Share Posted December 27, 2016 46 minutes ago, Tahseen said: Thank you very much! It's now working perfectly! I really appreciate your support to me! You're welcome. 1 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