MAB Posted December 4, 2015 Share Posted December 4, 2015 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? Link to comment
Moderators IIYAMA Posted December 5, 2015 Moderators Share Posted December 5, 2015 There are a lot of ways to do this. local a,b = string.match("1+3","(%d+)+(%d+)") outputChatBox(tonumber(a)+tonumber(b)) Link to comment
sanyisasha Posted December 5, 2015 Share Posted December 5, 2015 string.match or gsub. But try not use math in string. Example: 3+1 a = 3 b = 1 func = "plus" if func == plus then c = 3 + 1 end outputChatBox(a.." + "..b.." = "..c) Link to comment
n3wage Posted December 5, 2015 Share Posted December 5, 2015 Or even string = "1+3"; number = loadstring ( "return "..string )(); Link to comment
MAB Posted December 5, 2015 Author Share Posted December 5, 2015 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
Moderators IIYAMA Posted December 5, 2015 Moderators Share Posted December 5, 2015 The result of loadstring must be called. Link to comment
MAB Posted December 5, 2015 Author Share Posted December 5, 2015 The result of loadstring must be called. example? Link to comment
Moderators IIYAMA Posted December 5, 2015 Moderators Share Posted December 5, 2015 local theSum = "1+3" local result = loadstring ( "return "..theSum )(); local theNumber = result() -- call Link to comment
MAB Posted December 5, 2015 Author Share Posted December 5, 2015 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 IIYAMA Posted December 5, 2015 Moderators Share Posted December 5, 2015 (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 December 6, 2015 by Guest Link to comment
MAB Posted December 6, 2015 Author Share Posted December 6, 2015 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
Moderators IIYAMA Posted December 6, 2015 Moderators Share Posted December 6, 2015 My Facebook account is not for MTA, but you can pm me your skype name instead. Link to comment
MAB Posted December 8, 2015 Author Share Posted December 8, 2015 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 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now