Jump to content

string to number = nil


MAB

Recommended Posts

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

Link to comment
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) 

Link to comment
  • Moderators

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

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