Jump to content

string to number = nil


MAB

Recommended Posts

Posted

i got a string that is like that for example "1+3", when i do tonumber for that string it returns nil not 4, why? how to fix this?

You can find me here.

  • Moderators
Posted

There are a lot of ways to do this.

local a,b = string.match("1+3","(%d+)+(%d+)") 
outputChatBox(tonumber(a)+tonumber(b)) 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

let me be more clear.... i got a string that looks like this one "1+2-10/100*60-(1+8)" it isn't simple like "1+3" and that string keeps changing every time... now i need a code that can get the result of all of these strings... is it possible?

by the way... loadstring isn't working

You can find me here.

  • Moderators
Posted

The result of loadstring must be called.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted
local theSum = "1+3" 
local result = loadstring ( "return "..theSum )(); 
local theNumber = result() -- call 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
local theSum = "1+3" 
local result = loadstring ( "return "..theSum )(); 
local theNumber = result() -- call 

note : dataText is a sum...

Error: attempt to call local 'result' (a number value)

code : client :

function getResult () 
    if getElementData(localPlayer,"calculator") == true then 
       local dataText = getElementData(localPlayer,"calculatorText"); 
       local result = loadstring ( "return "..dataText )(); 
       local theNumber = result() 
        if theNumber then 
           setElementData(localPlayer,"calculatorText",theNumber) 
        else 
           setElementData(localPlayer,"calculatorText","Error") 
        end 
    end 
end 
bindKey("num_enter","down",getResult) 

You can find me here.

  • Moderators
Posted (edited)

ah I miss read, it was already called. And it did already work.

function getResult () 
    if getElementData(localPlayer,"calculator") == true then 
        local dataText = getElementData(localPlayer,"calculatorText") 
        local result = loadstring ( "return "..dataText ) 
        if result then  
            local theNumber = result() 
            if theNumber then 
               setElementData(localPlayer,"calculatorText",theNumber) 
            else 
               setElementData(localPlayer,"calculatorText","Error") 
            end 
        end 
    end 
end 
bindKey("num_enter","down",getResult) 

Edited by Guest

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
ah I miss read, it was already called. And it did already work.
function getResult () 
    if getElementData(localPlayer,"calculator") == true then 
        local dataText = getElementData(localPlayer,"calculatorText") 
        local result = loadstring ( "return "..dataText ) 
        if result then  
            local theNumber = result() 
            if theNumber then 
               setElementData(localPlayer,"calculatorText",theNumber) 
            else 
               setElementData(localPlayer,"calculatorText","Error") 
            end 
        end 
    end 
end 
bindKey("num_enter","down",getResult)l 

OMG thanks a lot man... my resource is almost done now... do you have a facebook account? i want to add you

You can find me here.

  • Moderators
Posted

My Facebook account is not for MTA, but you can pm me your skype name instead.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
My Facebook account is not for MTA, but you can pm me your skype name instead.

removed skype but anyway look at the script after i finished it.. here

You can find me here.

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