Jump to content

Attempt to perform arithmetic on a nil value


Tete omar

Recommended Posts

Hey guys i got a problem here

client

function buying() 
    local a=guiGetText(GOLD_lab) 
    local b=guiGetText(SILVER_lab) 
    local c=guiGetText(PLATINUM_lab) 
    local d=guiGetText(IRON_lab) 
    local e=guiGetText(ALUMINIUM_lab) 
    local f=guiGetText(COPPER_lab) 
    local g=guiGetText(PHOSPHATE_lab) 
    local h=guiGetText(LEAD_lab) 
    a_=string.gsub(a,"[^0-9]","") 
    b_=string.gsub(b,"[^0-9]","") 
    c_=string.gsub(c,"[^0-9]","") 
    d_=string.gsub(d,"[^0-9]","") 
    e_=string.gsub(e,"[^0-9]","") 
    f_=string.gsub(f,"[^0-9]","") 
    g_=string.gsub(g,"[^0-9]","") 
    h_=string.gsub(h,"[^0-9]","") 
    local a1=guiGetText(GOLD_am) 
    if not(a1=="")or not(a1=="0")then 
        local a2=(tonumber(a1))/tonumber(a_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,a2) 
    end 
    local b1=guiGetText(SILVER_am) 
    if not(b1=="")or not(b1=="0")then 
        local b2=(tonumber(b1))/tonumber(b_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,b2) 
    end 
    local c1=guiGetText(PLATINUM_am) 
    if not(c1=="")or not(c1=="0")then 
        local c2=(tonumber(c1))/tonumber(c_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,c2) 
    end 
    local d1=guiGetText(IRON_am) 
    if not(d1=="")or not(d1=="0")then 
        local d2=(tonumber(d1))/tonumber(d_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,d2) 
    end 
    local e1=guiGetText(ALUMINIUM_am) 
    if not(e1=="")or not(e1=="0")then 
        local e2=(tonumber(e1))/tonumber(e_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,e2) 
    end 
    local f1=guiGetText(COPPER_am) 
    if not(f1=="")or not(f1=="0")then 
        local f2=(tonumber(f1))/tonumber(f_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,f2) 
    end 
    local g1=guiGetText(PHOSPHATE_am) 
    if not(g1=="")or not(g1=="0")then 
        local g2=(tonumber(g1))/tonumber(g_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,g2) 
    end 
    local h1=guiGetText(LEAD_am) 
    if not(h1=="")or not(h1=="0")then 
        local h2=(tonumber(h1))/tonumber(h_) -- Attempt to perform arithmetic on a nil value 
        triggerServerEvent("BuyingMetals",resourceRoot,h2) 
    end 
end 

server

addEvent("BuyingMetals",true) 
addEventHandler("BuyingMetals",root, 
function( metalprice ) 
    if(getPlayerMoney(client)>=tonumber(metalprice))then 
        takePlayerMoney(client,tonumber(metalprice)) 
        outputChatBox("enougnmoney " .. metalprice) 
    else 
    outputChatBox("enougnmoney " .. metalprice) 
    end 
end) 

My whole problem is about " Attempt to perform arithmetic on a nil value "

Link to comment

"_lab" things is about a "gui-label"

and

"_am" things is about a "gui-edit"

local a=guiGetText(GOLD_lab) 
local a1=guiGetText(GOLD_am) 

One or both do not have an actual value, returns a nil value. This means check if the GUI element GOLD_lab or GOLD_am return any value when triggered.

I'm actually dividing "_am" things on "_lab" things as you can see here

local a2=(tonumber(a1))/tonumber(a_) 

You may wonder what "a_"

and it's defined as

a_=string.gsub(a,"[^0-9]","") 

and you may wonder what is "a"

it's defined as a gui-label

local a=gg(GOLD_lab) 

actually i used string.gsub to get the numbers out of the string , because the string is about characters and numbers.

Link to comment

In that case you have two same variables.

local a=guiGetText(GOLD_lab) 

and

local a=gg(GOLD_lab) 

Those two are both defined as 'a', which doesn't really work out, unless I understood you wrong. I think solidsnake has a resolution for this as soon as he sees this.

Link to comment
In that case you have two same variables.
local a=guiGetText(GOLD_lab) 

and

local a=gg(GOLD_lab) 

Those two are both defined as 'a', which doesn't really work out, unless I understood you wrong. I think solidsnake has a resolution for this as soon as he sees this.

resolution ?

.. anyway

the division result is a float number .. that's why nil or if not , i dunno

Link to comment

probably your guiGetText dont contain any numbers, so tonumber() returns nil

debug it, check the data you get from your gui elements

since your conditions are logically wrong (should be AND, not OR), you're always passing even empty strings to tonumber().

example (not lua):

if A is not 1 OR A is not 2 — result will always be true

when A IS 2, the first condition is true (because 2 is not 1)

when A IS 1, the second condition is true (because 1 is not 2)

and they are both true with any other A value

so you should either use AND:

if (a ~= "") and (a ~= "0") then  
  -- do stuff with your data 
end 

or change the condition and the code executed if its true:

if (a == "") or (a == "0") then  
  -- do stuff when NO VALID DATA (i.e. "a" doesnt contain numbers or is "0") is present 
else  
  -- do stuff with your data 
end  

Link to comment
probably your guiGetText dont contain any numbers, so tonumber() returns nil

debug it, check the data you get from your gui elements

since your conditions are logically wrong (should be AND, not OR), you're always passing even empty strings to tonumber().

example (not lua):

if A is not 1 OR A is not 2 — result will always be true

when A IS 2, the first condition is true

when A IS 1, the second condition is true

and they are both true with any other A value

so you should either use AND:

if (a ~= "") and (a ~= "0") then  
  -- do stuff with your data 
end 

or change the condition and the code executed if its true:

if (a == "") or (a == "0") then  
  -- do stuff when NO VALID DATA (i.e. "a" doesnt contain numbers or is "0") is present 
else  
  -- do stuff with your data 
end  

Thanks ,

after fixing

client

function buying() 
    local a=gg(GOLD_lab) 
    local b=gg(SILVER_lab) 
    local c=gg(PLATINUM_lab) 
    local d=gg(IRON_lab) 
    local e=gg(ALUMINIUM_lab) 
    local f=gg(COPPER_lab) 
    local g=gg(PHOSPHATE_lab) 
    local h=gg(LEAD_lab) 
    a_=string.gsub(a,"[^0-9]","") 
    b_=string.gsub(b,"[^0-9]","") 
    c_=string.gsub(c,"[^0-9]","") 
    d_=string.gsub(d,"[^0-9]","") 
    e_=string.gsub(e,"[^0-9]","") 
    f_=string.gsub(f,"[^0-9]","") 
    g_=string.gsub(g,"[^0-9]","") 
    h_=string.gsub(h,"[^0-9]","") 
    local a1=gg(GOLD_am) 
    if(a1~="")and(a1~="0")then  
        local a2=(tonumber(a1))/tonumber(a_) 
        triggerServerEvent("BuyingMetals",resourceRoot,a2) 
    end 
    local b1=gg(SILVER_am) 
    if(b1~="")and(b1~="0")then 
        local b2=(tonumber(b1))/tonumber(b_) 
        triggerServerEvent("BuyingMetals",resourceRoot,b2) 
    end 
    local c1=gg(PLATINUM_am) 
    if(c1~="")and(c1~="0")then 
        local c2=(tonumber(c1))/tonumber(c_) 
        triggerServerEvent("BuyingMetals",resourceRoot,c2) 
    end 
    local d1=gg(IRON_am) 
    if(d1~="")and(d1~="0")then 
        local d2=(tonumber(d1))/tonumber(d_) 
        triggerServerEvent("BuyingMetals",resourceRoot,d2) 
    end 
    local e1=gg(ALUMINIUM_am) 
    if(e1~="")and(e1~="0")then 
        local e2=(tonumber(e1))/tonumber(e_) 
        triggerServerEvent("BuyingMetals",resourceRoot,e2) 
    end 
    local f1=gg(COPPER_am) 
    if(f1~="")and(f1~="0")then 
        local f2=(tonumber(f1))/tonumber(f_) 
        triggerServerEvent("BuyingMetals",resourceRoot,f2) 
    end 
    local g1=gg(PHOSPHATE_am) 
    if(g1~="")and(g1~="0")then 
        local g2=(tonumber(g1))/tonumber(g_) 
        triggerServerEvent("BuyingMetals",resourceRoot,g2) 
    end 
    local h1=gg(LEAD_am) 
    if(h1~="")and(h1~="0")then 
        local h2=(tonumber(h1))/tonumber(h_) 
        triggerServerEvent("BuyingMetals",resourceRoot,h2) 
    end 
end 

server

addEvent("BuyingMetals",true) 
addEventHandler("BuyingMetals",root, 
function( metalprice ) 
    if(getPlayerMoney(client)>=tonumber(metalprice))then 
        takePlayerMoney(client,tonumber(metalprice)) 
        outputChatBox("testing " .. metalprice) 
    end 
end) 

the code is fine and no errors but doesn't take the player's money

and i only see float values on the chatbox.

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