Numbers dont work correctly

Greetings all.
I wanna test something with transparency but numbers are going wrong. here is the script:

local frame = script.Parent.Frame
frame.Transparency = 1
repeat 
	frame.Transparency -= 0.1
	wait(0.1)
	print(frame.Transparency)
until frame.Transparency == 0 or isPlaying == false

And here is an output

  10:58:48.746  0.8999999761581421  -  Client - BlackScreen+Run:31
  10:58:48.862  0.7999999523162842  -  Client - BlackScreen+Run:31
  10:58:48.979  0.6999999284744263  -  Client - BlackScreen+Run:31
  10:58:49.095  0.5999999046325684  -  Client - BlackScreen+Run:31
  10:58:49.212  0.49999991059303284  -  Client - BlackScreen+Run:31
  10:58:49.312  0.3999999165534973  -  Client - BlackScreen+Run:31
  10:58:49.430  0.2999999225139618  -  Client - BlackScreen+Run:31
  10:58:49.545  0.19999992847442627  -  Client - BlackScreen+Run:31
  10:58:49.645  0.09999992698431015  -  Client - BlackScreen+Run:31
  10:58:49.746  -7.301569127093899e-08  -  Client - BlackScreen+Run:31

Why does transparency goes 0.899, 0.799 and etc but not 0.9, 0.8, 0.7 and etc and then it goes on -7 but not goes to zero? The same thing with game.Workspace.Camera.FieldOfView . Here is the script

local gameCamera = game.Workspace.Camera
gameCamera.FieldOfView = 95
repeat
	gameCamera.FieldOfView -= 2.5
	wait(0.01)
	print(gameCamera.FieldOfView)
until gameCamera.FieldOfView == 70 or isPlaying == true

And an output

  10:58:49.762  92.50000762939453  -  Client - BlackScreen+Run:49
  10:58:49.795  90.00000762939453  -  Client - BlackScreen+Run:49
  10:58:49.829  87.5  -  Client - BlackScreen+Run:49
  10:58:49.862  84.99999237060547  -  Client - BlackScreen+Run:49
  10:58:49.895  82.49999237060547  -  Client - BlackScreen+Run:49
  10:58:49.929  79.99998474121094  -  Client - BlackScreen+Run:49
  10:58:49.962  77.49998474121094  -  Client - BlackScreen+Run:49
  10:58:49.996  74.99998474121094  -  Client - BlackScreen+Run:49
  10:58:50.029  72.49998474121094  -  Client - BlackScreen+Run:49
  10:58:50.062  69.99998474121094  -  Client - BlackScreen+Run:49

Is there any solutions how to make them 92.5, 90, 87.5, 85 and why they are going with many numbers after dot?

1 Like

Because of floating point error. Do until frame.Transparency <= 0 or until
approxEquals(frame.Transparency, 0)` then define approxEquals like this:

local MAX_DIFFERENCE = 1e-5
function approxEquals(a, b)
    return (b - a) <= MAX_DIFFERENCE
end

or use a for loop.

for t = 1, 0, -0.1 do
    frame.Transparency = t
    wait(0.1)
    if isPlaying then break end
end
3 Likes

Do math.round(frame.Transparency*10)/10

Instead of using wait() use task.wait()

local frame = script.Parent.Frame
frame.Transparency = 1
repeat 
	frame.Transparency -= math.round(frame.Transparency*10)/10
	task.wait(0.1)
	print(frame.Transparency)
until frame.Transparency == 0 or isPlaying == false
local gameCamera = game.Workspace.Camera
gameCamera.FieldOfView = 95
repeat
	gameCamera.FieldOfView -= math.round(gameCamera.FieldOfView*10)/10
	task.wait(0.01)
	print(gameCamera.FieldOfView)
until gameCamera.FieldOfView == 70 or isPlaying == true

This should help.

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