Value kept getting 0 with this script

I must say that I’m still new to Lua and, my maths aren’t right neither.
On this script, i want it to increase them m value by +.01 or decrease by -.01.

m=.1
script.Parent.MouseButton1Down:connect(function()
    m += .01
    m*= 10
    m = math.floor(m)
    m = m / 10
    script.Parent.Text = m
    script.Parent.Parent.DelayValue.Value = m
end)
script.Parent.MouseButton2Down:connect(function()
    if m >= 0.01 then
    m -= .01
    m*= 10
    m = math.floor(m)
    m = m / 10
    script.Parent.Text = m
        script.Parent.Parent.DelayValue.Value = m
    end
end)
2 Likes

This is what the script is doing:

  • For the increase: First you add 0.01 to m, making m = 0.11. Then you multiply m by 10 (making m = 1.1) then use math.floor on m (which returns the biggest integer smaller than m), and this returns 1. Finally you divide m by 10, and the final result of m is 1/10 = 0.1 (the same thing).
  • For the decrease: Subtracting 0.01 from m makes it equal to 0.09. Going through the same thing as you did above, you multiplied m by 10, using math.floor on m then divide it by 10. At first it may seem normal, making m = 0.9 after the multiplication, however due to math.floor’s logic it rounds down m to 0 instead.

The code was probably your attempt to avoid floating point errors, but this is incorrect. The final code should look like this:

m=.1
script.Parent.MouseButton1Down:connect(function()
    m += .01
    m *= 100 -- 11, you should make this final result not a decimal
m = math.floor(m) -- still 11
m = m / 100 -- 0.11
    script.Parent.Text = m
    script.Parent.Parent.DelayValue.Value = m
end)
script.Parent.MouseButton2Down:connect(function()
    if m >= 0.01 then
    m -= .01
    m*= 100 -- 9
    m = math.floor(m) -- still 9
    m = m / 100 -- 0.09
    script.Parent.Text = m
        script.Parent.Parent.DelayValue.Value = m
    end
end)

Sorry for the long reply

3 Likes

Hey there, thank you for the solution! But there is a problem.
I used Button2Down until i reach 0, but then when i use Button1Down to increase the value, it works only until it reach the value 0.6

EDIT: I found the fix, i just need to use math.round instead of math.floor

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.