Jump to content

Tables Issue


Tahseen

Recommended Posts

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

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 by mgmmcaobbm
  • Like 1
Link to comment
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
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 by mgmmcaobbm
Link to comment
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
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 by mgmmcaobbm
added tut link
  • Like 1
Link to comment
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

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