Incorrect incrementing

everything is fine with a script i made but it seems that its not incrementing by 0.1 correctly, the max number should be 1 but is instead 1.09999999…

	elseif input.KeyCode == Enum.KeyCode.E then
		if throttle < 1 then
			EDown = true
			while EDown and task.wait(0.1) do
				if throttle < 1 then
					throttle += 0.1
					print(throttle)
				else break end
			end
		end

image

The issue is not with your script but rather with how computers themselves represent floating points. Computers uses zero’s and one’s to represent floating points which makes it hard to represent the number accurately. And Lua uses only 32 bytes for decimal numbers which is not enough to store a perfect decimal. However, these inaccuracies are tiny tiny tiny marginals… and your script will not suffer because of it.

Can you show a snippit of the code where you instantiate the “throttle” variable?

local throttle = 0

runService.Heartbeat:Connect(function()
	if throttle == 0 then return end
	local Direction = vs.CFrame.lookVector
	bodyvelocity.VectorVelocity = Direction * (maxSpeed * throttle)
end)

local engineOn = false

local WDown = false
local SDown = false
local ADown = false
local DDown = false
local EDown = false
local QDown = false

UIS.InputBegan:Connect(function(input)
	if (not engineOn) then return end
	if input.KeyCode == Enum.KeyCode.W then
		WDown = true
		angularVelocity.AngularVelocity += Vector3.new(1,0,0)
	elseif input.KeyCode == Enum.KeyCode.S then
		SDown = true
		angularVelocity.AngularVelocity += Vector3.new(-1,0,0)
	elseif input.KeyCode == Enum.KeyCode.A then
		ADown = true
		angularVelocity.AngularVelocity += Vector3.new(0,1,0)
	elseif input.KeyCode == Enum.KeyCode.D then
		DDown = true
		angularVelocity.AngularVelocity += Vector3.new(0,-1,0)
	elseif input.KeyCode == Enum.KeyCode.E then
		if throttle < 1 then
			EDown = true
			while EDown and task.wait(0.1) do
				if throttle < 1 then
					throttle += 0.1
					print(throttle)
				else break end
			end
		end

i know about the tiny difference but when the throttle reaches 0.9999 another 0.1 will be added since its not yet 1

If there’s a problem with throttle surpassing 1 you can use math.max like so

	elseif input.KeyCode == Enum.KeyCode.E then
		if throttle < 1 then
			EDown = true
			while EDown and task.wait(0.1) do
				if throttle < 1 then
					-- Ensures throttle stays between 0.1 and 1
					throttle = math.max(throttle+0.1, 1)
					print(throttle)
				else break end
			end
		end

@starmarine3355 is correct, you can just

math.floor(throttle * 10 + 0.5) / 10) -- 10 sets how many decimals to round to!

To figure how many decimals you want it rounded to, you do

10^(decimalsyouwant) -- this would replace both 10's in the example above.

@starmarine3355 's solution doesnt seem to work as its setting it to 1 on the first second but for your solution where would i paste that? thanks again

Oops, wrong function. Meant the clamp

throttle = math.clamp(throttle+0.1, 0, 1)
if throttle < 1 then
			EDown = true
			while EDown and task.wait(0.1) do
				if throttle < 1 then
                    throttle = math.floor(throttle * 10 + 0.5) / 10 +.1 )
					print(throttle)
				else break end
			end
		end
1 Like

yep looks like math.clamp was the solution, thanks @starmarine3355 & @NobleBuild

throttle = math.clamp(throttle+0.1, 0, 1)

image

2 Likes