I'm making a script that uses an equation to calculate the distance between 2 objects, then moves 1 object to the other in a certain Time, and the speed at which it goes to the other part is dependent on the Time. Problem is, these equations will often give numbers with a crazy amount of numbers after the decimal, this causes all sorts of weirdness, such as the part missing its target. How would I round up the numbers so I don't get such crazy numbers?
--Cup goes to position 1 local Time=4 local Distance=(math.abs(Cup.Position.X+Pos1.Position.X)) local StudsPer=(Time/Distance) local TimeUp=0 ------------ repeat wait(StudsPer) Cup.CFrame=Cup.CFrame*CFrame.new(0,0,StudsPer) TimeUp=TimeUp+StudsPer until TimeUp>=Time do Cup.Position=Pos1.Position--Ensures it gets there in case of bugs
Rounding numbers is easy,
local n = 1.5 print(math.ceil(n))
will output:
2
and
local n = 1.5 print(math.floor(n))
will output:
1
Sources: math.floor
There are two methods to rounding the two are, math.ceil()
and math.floor()
. as you can imagine ceil is short for ceiling so it rounds up, and floor is well... floor, so it rounds down. These are very simple to understand.
Example:
local rounddown = 12.5 local roundup = 12.5 print(math.ceil(roundup)) --prints 13 print(math.floor(rounddown)) --prints 12