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
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.
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
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 '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
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