Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Roblox not assigning accurate variables correctly?

Asked by
bowanw 16
7 years ago

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
0
Poke (Look below, new comment on my answer) RubenKan 3615 — 7y
0
Poke again RubenKan 3615 — 7y
0
Poke Again. RubenKan 3615 — 7y

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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
0
Thankyou for your reply, however this still gave me the same output for both methods with decimals. bowanw 16 — 7y
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. RubenKan 3615 — 7y
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. bowanw 16 — 7y
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) --] RubenKan 3615 — 7y
View all comments (3 more)
0
This is more manipulating the print and not the actual variable. I wouldn't really call this a work around as such. bowanw 16 — 7y
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)) RubenKan 3615 — 7y
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. RubenKan 3615 — 7y
Ad

Answer this question