Jump to content

Need help converting string


jkub

Recommended Posts

I would usually know how to convert a string into a number normally using tostring or tonumber but now I am getting data from a gui gridlist I have created and now I cannot do anything.

I am storing a "price" in one of the grid list rows on the "priceColumn" and I use guiGridListGetItemText to retrieve the price that is displayed inside the row on the price column. It returns a string So I am able to output the correct thing to the chatbox but when I try to take it convert it to a number using tonumber it comes back as nil.

Attempt to compare nil with number is what I get.

here is some of my code

function clickHandler ( button )
if source == shopWindow_Close then
guiSetVisible ( shopWindow_Wang, false )
showCursor ( false )
removeEventHandler ( "onClientRender", getRootElement(), frameHandler )
destroyElement ( dummyCar )
elseif source == shopWindow_Purchase then
local money = getPlayerMoney ( getLocalPlayer() )
if ( money >= price ) then
outputChatBox ( "You can afford these hax!", 0, 255, 0 )
else
outputChatBox ( "You cannot afford these hax...", 255, 0, 0 )
end
end
end
 
function gridClickHandler ( button )
row, column = guiGridListGetSelectedItem ( wangList )
local data = guiGridListGetItemText ( wangList, row, vehicleColumn )
local pricereturn = guiGridListGetItemText ( wangList, row, priceColumn )
if row then
setElementModel ( dummyCar, getVehicleIDFromName ( tostring(data) ) )
	price = tostring(pricereturn)
outputChatBox ( price )
end
end

Btw I am trying yet again to make a little vehicle shop type deal. So you might know where I might be going

Please help

Link to comment

I would usually know how to convert a string into a number normally using tostring or tonumber but now I am getting data from a gui gridlist I have created and now I cannot do anything.

I am storing a "price" in one of the grid list rows on the "priceColumn" and I use guiGridListGetItemText to retrieve the price that is displayed inside the row on the price column. It returns a string So I am able to output the correct thing to the chatbox but when I try to take it convert it to a number using tonumber it comes back as nil.

Attempt to compare nil with number is what I get.

here is some of my code

function clickHandler ( button )
if source == shopWindow_Close then
guiSetVisible ( shopWindow_Wang, false )
showCursor ( false )
removeEventHandler ( "onClientRender", getRootElement(), frameHandler )
destroyElement ( dummyCar )
elseif source == shopWindow_Purchase then
local money = getPlayerMoney ( getLocalPlayer() )
if ( money >= price ) then
outputChatBox ( "You can afford these hax!", 0, 255, 0 )
else
outputChatBox ( "You cannot afford these hax...", 255, 0, 0 )
end
end
end
 
function gridClickHandler ( button )
row, column = guiGridListGetSelectedItem ( wangList )
local data = guiGridListGetItemText ( wangList, row, vehicleColumn )
local pricereturn = guiGridListGetItemText ( wangList, row, priceColumn )
if row then
setElementModel ( dummyCar, getVehicleIDFromName ( tostring(data) ) )
	price = tostring(pricereturn)
outputChatBox ( price )
end
end

Btw I am trying yet again to make a little vehicle shop type deal. So you might know where I might be going

Please help

Link to comment

which line is the actual error on?

i cant see much we can do with the code you provided, can you also show us your gridlist text?

by the looks of it your 'price' variable is global, so if thats your problem variable then make sure it actually has a value

theres no reason why tonumber would return nil if you are actually passing a numeric string, make sure there are no currency signs, erroneous spaces, etc as well

Link to comment

which line is the actual error on?

i cant see much we can do with the code you provided, can you also show us your gridlist text?

by the looks of it your 'price' variable is global, so if thats your problem variable then make sure it actually has a value

theres no reason why tonumber would return nil if you are actually passing a numeric string, make sure there are no currency signs, erroneous spaces, etc as well

Link to comment

it says at line 100 "attempt to compare string with number"

ive already tried using if ( money >= tonumber(price) ) then

but that just makes it return nil like I said

function clickHandler ( button )
if source == shopWindow_Close then
guiSetVisible ( shopWindow_Wang, false )
showCursor ( false )
removeEventHandler ( "onClientRender", getRootElement(), frameHandler )
destroyElement ( dummyCar )
elseif source == shopWindow_Purchase then
local money = getPlayerMoney ( getLocalPlayer() )
if ( money >= price ) then--!!!in my script this line here is line 100
outputChatBox ( "You can afford these hax!", 0, 255, 0 )
else
outputChatBox ( "You cannot afford these hax...", 255, 0, 0 )
end
end
end
 
function gridClickHandler ( button )
row, column = guiGridListGetSelectedItem ( wangList )
local data = guiGridListGetItemText ( wangList, row, vehicleColumn )
local pricereturn = guiGridListGetItemText ( wangList, row, priceColumn )
if row then
setElementModel ( dummyCar, getVehicleModelFromName ( tostring(data) ) )
	price = tostring(pricereturn)
outputChatBox ( price )
end
end

here is some gridlist code

vehicleColumn = guiGridListAddColumn ( wangList, "Vehicle", 0.3 )
priceColumn = guiGridListAddColumn ( wangList, "Price", 0.2 )
       row1 = guiGridListAddRow ( wangList )
guiGridListSetItemText ( wangList, row1, priceColumn, "$86,140", false, true )

at first I thought it would of been because I set the last boolean on setItemText to false which I think has somthing to do with rather the number is a number value so I set it to true... but still no luck

Link to comment

it says at line 100 "attempt to compare string with number"

ive already tried using if ( money >= tonumber(price) ) then

but that just makes it return nil like I said

function clickHandler ( button )
if source == shopWindow_Close then
guiSetVisible ( shopWindow_Wang, false )
showCursor ( false )
removeEventHandler ( "onClientRender", getRootElement(), frameHandler )
destroyElement ( dummyCar )
elseif source == shopWindow_Purchase then
local money = getPlayerMoney ( getLocalPlayer() )
if ( money >= price ) then--!!!in my script this line here is line 100
outputChatBox ( "You can afford these hax!", 0, 255, 0 )
else
outputChatBox ( "You cannot afford these hax...", 255, 0, 0 )
end
end
end
 
function gridClickHandler ( button )
row, column = guiGridListGetSelectedItem ( wangList )
local data = guiGridListGetItemText ( wangList, row, vehicleColumn )
local pricereturn = guiGridListGetItemText ( wangList, row, priceColumn )
if row then
setElementModel ( dummyCar, getVehicleModelFromName ( tostring(data) ) )
	price = tostring(pricereturn)
outputChatBox ( price )
end
end

here is some gridlist code

vehicleColumn = guiGridListAddColumn ( wangList, "Vehicle", 0.3 )
priceColumn = guiGridListAddColumn ( wangList, "Price", 0.2 )
       row1 = guiGridListAddRow ( wangList )
guiGridListSetItemText ( wangList, row1, priceColumn, "$86,140", false, true )

at first I thought it would of been because I set the last boolean on setItemText to false which I think has somthing to do with rather the number is a number value so I set it to true... but still no luck

Link to comment

Thanks That worked:) But... Now that I have done that in my guigrid list the price it just says 86140 instead of $86,140. is there a way I can have it displayed in the gui with the dollar sign and the comma with out it messing up the rest of the script when It comes to when I have to compare both the values again?

Link to comment

Thanks That worked:) But... Now that I have done that in my guigrid list the price it just says 86140 instead of $86,140. is there a way I can have it displayed in the gui with the dollar sign and the comma with out it messing up the rest of the script when It comes to when I have to compare both the values again?

Link to comment
Thanks That worked:) But... Now that I have done that in my guigrid list the price it just says 86140 instead of $86,140. is there a way I can have it displayed in the gui with the dollar sign and the comma with out it messing up the rest of the script when It comes to when I have to compare both the values again?

You can use grid list item data to contain price as number and text to display $ sign.

https://wiki.multitheftauto.com/wiki/Gui ... etItemData

Link to comment
Thanks That worked:) But... Now that I have done that in my guigrid list the price it just says 86140 instead of $86,140. is there a way I can have it displayed in the gui with the dollar sign and the comma with out it messing up the rest of the script when It comes to when I have to compare both the values again?

You can use grid list item data to contain price as number and text to display $ sign.

https://wiki.multitheftauto.com/wiki/Gui ... etItemData

Link to comment

I tried this to set the data

guiGridListSetItemData ( wangList, row1, priceColumn, 86140 )

and this to retrieve it

local price = guiGridListGetItemData ( wangList, row1, priceColumn )

then I try if ( money >= price ) then

but that returns attempt to compare nil with number

Link to comment

I tried this to set the data

guiGridListSetItemData ( wangList, row1, priceColumn, 86140 )

and this to retrieve it

local price = guiGridListGetItemData ( wangList, row1, priceColumn )

then I try if ( money >= price ) then

but that returns attempt to compare nil with number

Link to comment

I got it to work eventually and did the longer way of manually coding in the prices. Right now it works pretty good and I havent seen any errors in the debug box yet. later I was thinking about putting the prices in an xml file.

Thanks guys ill be back:)

Link to comment

I got it to work eventually and did the longer way of manually coding in the prices. Right now it works pretty good and I havent seen any errors in the debug box yet. later I was thinking about putting the prices in an xml file.

Thanks guys ill be back:)

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