Jump to content

Convert the number to string return with error


KariiiM

Recommended Posts

Posted

Hello guys, i have a small problem maybe you know how to handle it, i tried to convert the money number to string to shows it in the gridlist like that $1,000, it shows in the gridlist but when i try to buy something it returns with this Error,

Error: attempt to compare nil with number

the Error is in this line:

guiGridListSetItemText (Grid, row, 2, convertNumber(v[2]), false, false) 

Here's the part of my code:

client side:

  function convertNumber ( number )   
    local formatted = number   
    while true do       
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')     
        if ( k==0 ) then       
            break    
        end   
    end   
    formatted = "$"..tostring(formatted) 
    return formatted 
end  
   
addEvent("callGUI",true) 
addEventHandler("callGUI",root, 
function (Table) 
  if not guiGetVisible(window) then 
   guiSetVisible(window,true) 
   showCursor(true) 
   guiGridListClear(Grid) 
   for i,v in ipairs (Table) do 
  local weapName = getWeaponNameFromID (v[1]) 
  local row = guiGridListAddRow (Grid) 
    guiGridListSetItemText (Grid, row, 1, weapName, false, false) 
    guiGridListSetItemText (Grid, row, 2, convertNumber(v[2]), false, false) 
    guiGridListSetItemText (Grid, row, 3, v[3], false, false) 
      end 
   end 
end) 
  
addEventHandler ("onClientGUIClick", root, 
function () 
        if (source == cancel) then 
        guiSetVisible(window,false) 
        showCursor(false) 
   elseif (source == create) then 
   local row,col = guiGridListGetSelectedItem(Grid) 
        if row and col and row ~= -1 and col ~= -1 then 
      guiSetVisible(window,false) 
      showCursor(false)  
     if (guiGridListGetSelectedItem (Grid)) then 
      local Name = guiGridListGetItemText (Grid, guiGridListGetSelectedItem (Grid), 1) 
      local ID = getWeaponIDFromName (Name) 
      local Cost = guiGridListGetItemText (Grid, guiGridListGetSelectedItem (Grid), 2) 
      local Ammount = guiGridListGetItemText (Grid, guiGridListGetSelectedItem (Grid), 3) 
      triggerServerEvent ("buy", getLocalPlayer(), ID, Cost, Name, Ammount) 
        end  
           else  
          outputChatBox("You didn't selecte the item.",255,0,0)    
       end 
    end 
end) 

--Server side: (Thetable)

local Table = { 
{31,100,250},{30,100,250},{28,100,100},{32,100,100},{34,700,150},{33,400,100},{16,500,50} 
} 
  
function onMarkerHit(hitPlayer) 
    if getElementType(hitPlayer) == "player" then 
     triggerClientEvent(hitPlayer,"callGUI",hitPlayer,Table) 
    end 
end 
  
addEvent ("buy", true) 
addEventHandler ("buy", getRootElement(),  
function(id, cost, name, ammo) 
local id = tonumber(id) 
local cost = tonumber(cost) 
local name = tostring(name) 
local ammo = tonumber(ammo) 
  if (getPlayerMoney (source) >= cost) then 
    Timer = setTimer(takePlayerMoney,22500,1,source,cost) 
    triggerClientEvent("progress",source) 
    TimerX = setTimer(giveWeapon,22500,1,source, id,ammo) 
    setElementData(source, "name", name) 
    setElementData(source, "ammo", ammo) 
     else 
   triggerClientEvent("remove",source) 
   outputChatBox ("You need "..cost.."$ to buy"..name..".", source, 255, 0, 0, false) 
  end 
end) 

Regards,

KariM

Posted
string.gsub  

already used, in the convertnumber function i think no need to do it two time

function convertNumber ( number )   
    local formatted = number   
    while true do       
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')     
        if ( k==0 ) then       
            break   
        end   
    end   
    formatted = "$"..tostring(formatted) 
    return formatted 
end 

Posted

lol i mean you need to use string.gsub to remove the ($ and , ) before you buy something.

Example:

local text = "$5000" 
string.gsub(text,"$"," ") 

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted
You shouldn't check for money on the client-side, use the table to check for the price server-side.

I tried something like that but i failed, can you please give an example?

Posted
money = tonumber(money:gsub("$", ""):gsub(",", "")) 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast

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