Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Roblox not assigning accurate variables correctly?

I'm trying to test a fan using surface motors, and have it slowly increase from 0% to 100%, taking 10 seconds in total to speed up. My problem is that when I print the ParamB for the motor surface, It leaves something like

0.0099999997764826
0.019999999552965
0.029999999329448
0.03999999910593
0.050000000745058
0.060000002384186
0.070000000298023

instead of

0.01
0.02
0.03
0.04
0.05
0.06
0.07

I am a bit of a perfectionist, so this ticks me off. Here is my code:

script.Parent.TopParamB = 0
while script.Parent.TopParamB < 1 do
script.Parent.TopParamB = script.Parent.TopParamB + .01
print(script.Parent.TopParamB)
wait(.1)
end

3 comments

0

Poke (Look below, new comment on my answer)

0

Poke again

0

Poke Again.

1 answer

This happens because roblox uses floating points.

The way you can get around this is to floor the for loop (use for i=0,100 do), or use the following.

for i=0,1,0.01 do
	script.Parent.TopParamB = math.floor(i*100)/100
	wait()
end

or the floored for loop

for i=0,100 do
	script.Parent.TopParamB = i/100
	wait()
end

7 comments

0

Thankyou for your reply, however this still gave me the same output for both methods with decimals.

0

I used both of those code in the command bar, and made them print the value of i/100 and the floored method, instead of asigning it to a Motor6D. It works fine. This means that its just how Motor6D takes the values. There's nothing you can do about it.

0

Alright, can you send me a copy of the full command you used so I can check them both, I may have formatted it wrong.

0

Its litheraly those two things but instead of [-- script.Parent.TopParamB = i/100 --] its [-- print(i/100) --] and [-- print(math.floor(i*100)/100) --]

0

This is more manipulating the print and not the actual variable. I wouldn't really call this a work around as such.

0

As i said, The motor6D is getting the value of 0.01, but the instance itself converts it to 0.0099999997764826. There is NOTHING you can do about it. (And to be honest, The value is SO SMALL, that you CAN'T even see a change. (Between 0.0099999997764826 or 0.01))

0

Also, if you don't realise that what you want to accomplish is 1. Irrelevent to the outcome. 2. Impossible (as its the Motor6D instance, not the value you assign to it.), then i'll just lock this question.

This discussion is preserved as it was. New answers, comments, and votes are closed.