I have a piece of code that counts in a for loop but while counting I want it so whenever it is divisible by 1 to run a function. However, it keeps failing the 'checking if divisible by 1' part (it gives a different number). I just can't figure it out, unless it's some bug.
Here's a slightly edited copy of my script:
for i = 0,Time, 0.1 do
Charge = i / Time
if i % 1 == 0 then
print(i, true, i % 1)
Function()
else
print(i, false, i % 1)
end
if HoldingZ == false then
break
end
wait(.1)
end
And here's the output:
> 0 true 0
> 0.1 false 0.1
> 0.2 false 0.2
> 0.3 false 0.3
> 0.4 false 0.4
> 0.5 false 0.5
> 0.6 false 0.6
> 0.7 false 0.7
> 0.8 false 0.8
> 0.9 false 0.9
> 1 false 1 -- Why does it not output 0?
> 1.1 false 0.1
> 1.2 false 0.2
> 1.3 false 0.3
> 1.4 false 0.4
> 1.5 false 0.5
> 1.6 false 0.6
> 1.7 false 0.7
> 1.8 false 0.8
> 1.9 false 0.9
> 2 false 4.4408920985006e-16 -- Why does it not output 0?
> 2.1 false 0.1
> 2.2 false 0.2
> 2.3 false 0.3
> 2.4 false 0.4
> 2.5 false 0.5
> 2.6 false 0.6
> 2.7 false 0.7
> 2.8 false 0.8
> 2.9 false 0.9
> 3 false 1.3322676295502e-15 -- Why does it not output 0?
> 3.1 false 0.1
> 3.2 false 0.2
> 3.3 false 0.3
> 3.4 false 0.4
> 3.5 false 0.5
> 3.6 false 0.6
> 3.7 false 0.7
> 3.8 false 0.8
> 3.9 false 0.9
> 4 false 1.7763568394003e-15 -- Why does it not output 0?
> 4.1 false 0.1
> 4.2 false 0.2
> 4.3 false 0.3
> 4.4 false 0.4
> 4.5 false 0.5
> 4.6 false 0.6
> 4.7 false 0.7
> 4.8 false 0.8
> 4.9 false 0.9
> 5 false 1 -- Why does it not output 0?
Now here's what happens if I do this in the command line at the bottom of Roblox Studio:
print(5%1) -- Output: 0
What am I doing wrong? I am absolutely confused!
Thanks for helping!
Solution on DevForum: https://devforum.roblox.com/t/modulus-not-working-properly-in-loops/874087/2

2 comments
Glad to hear that this was solved! :D
Glad to hear that this was solved! :D