If a certain variable is greater then or equal to a certain variable do this function

Hey guys how do I make it so when a variable is greater then a certain variable do this function. Here is what i’ve tried.

local function saveScore( event ) --The variable values are up above. if M.score \> text.text then score.subtract(event.target.value) else score.save() end

What are M.score, text.text and event.target.value?

Without knowing what those are or what the variables are, where event originates from, etc.

Rob

@tpjacobson01

i think what rob is getting at is we cant tell the values or the types of your variables above.

If you are expecting your values to be numbers but are treating them as a string then the above is not going to work very well

Try converting your variables to numbers like this

tonumber(line)   – try to convert it to a number

if tonumber (M.score)  > tonumber (text.text)  then
               
           score.subtract(event.target.value)
 else
                    score.save()
end

Good luck

Actually I meant two things…   1. what are the values?   and 2.  It’s kinda odd to see M.someVariable and event.target.value together in the same code.  Typically M.something is code in a module, but score.subtract sounds like code outside of a module and if this is based on the score tutorial, I don’t remember saveScore taking an event (though it’s been a while since I looked at that code), normally it’s just given a value, so I don’t know where event.target.value is coming from.

@doubleslashdesign @Rob Miracle Both the variables are integers M.score = 10,000 at first then changes and text.text = math.random. And yes the m.score is from the score module.

What is event.target.value and where does it come from?

@Rob Miracle the event.target value comes from the score module. The target value is text.text which equals math.random

--from score.lua function M.subtract( amount ) M.score = M.score - text.text M.scoreText.text = string.format(M.format, M.score) end

I figured it out!!!

I just had to use the “tonumber” function

What are M.score, text.text and event.target.value?

Without knowing what those are or what the variables are, where event originates from, etc.

Rob

@tpjacobson01

i think what rob is getting at is we cant tell the values or the types of your variables above.

If you are expecting your values to be numbers but are treating them as a string then the above is not going to work very well

Try converting your variables to numbers like this

tonumber(line)   – try to convert it to a number

if tonumber (M.score)  > tonumber (text.text)  then
               
           score.subtract(event.target.value)
 else
                    score.save()
end

Good luck

Actually I meant two things…   1. what are the values?   and 2.  It’s kinda odd to see M.someVariable and event.target.value together in the same code.  Typically M.something is code in a module, but score.subtract sounds like code outside of a module and if this is based on the score tutorial, I don’t remember saveScore taking an event (though it’s been a while since I looked at that code), normally it’s just given a value, so I don’t know where event.target.value is coming from.

@doubleslashdesign @Rob Miracle Both the variables are integers M.score = 10,000 at first then changes and text.text = math.random. And yes the m.score is from the score module.

What is event.target.value and where does it come from?

@Rob Miracle the event.target value comes from the score module. The target value is text.text which equals math.random

--from score.lua function M.subtract( amount ) M.score = M.score - text.text M.scoreText.text = string.format(M.format, M.score) end

I figured it out!!!

I just had to use the “tonumber” function