How to fix subtraction

Hello
I have an issue with the result of 16 - 0.2.
I’m trying to subtract 0.2 from the walk speed, but the result is:16.200001525878907

Script:

plrModel.Humanoid.WalkSpeed -= NumberValue.Value
2 Likes

Double-check if you are passing a negative value. because it may pass it like this:

--  A
local num = 16
function subtract(val)
    return num -= val
end

print(subtract(-2)) --// 16.2

So, lets check if its negative instead:

local num = 16
function subtract(val)
    if val > 0 then
        return num -= val
    end
end

print(subtract(-2)) --// nil
1 Like

An easier solution would be to use math.abs like so:

plrModel.Humanoid.WalkSpeed -= math.abs(NumberValue.Value)

if the value can be negative but @napastnikiek1 always wants to subtract

4 Likes
plrModel.Humanoid.WalkSpeed -= -NumberValue.Value

if the numbervalue is a negative then the difference between walkspeed and the numbervalue will count as an addition which is why it gives this result.

alternatively, you can use “+=” as it also counts as a difference if the numbervalue is a negative number.

plrModel.Humanoid.WalkSpeed += NumberValue.Value
1 Like

tnx @JohhnyLegoKing, I don’t want to just subtract a negative number
the result is now correct and sorry for my english I use a translator

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.