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

If CFrame.Position isn't a certain number then?

Asked by
iFlusters 355 Moderation Voter
8 years ago

Not very experienced with CFrame and Parts, so I'll explain anyway, basically I want to check to see if something isn't at a certain Y value and if it is not it will change, here's what I mean:

function CheckYValue()
    while wait() do
        if Part.CFrame.Y ~= 458.4 then -- I think this is the problem..
            Part.CFrame = Part.CFrame * CFrame.new(0, 0, 0.1) -- I can do this bit!
    else
        return
    end
end
0
Could you give a bit more context of what you are trying to do with the code? NullSenseStudio 342 — 8y
0
The if statement on line 3, if the Part Y Value isn't at 458.4 then it will run the code. iFlusters 355 — 8y
0
I get that but what is the code doing that you are not expecting it to? NullSenseStudio 342 — 8y
0
It moves the part up constantly, as shown on line 4, it doesn't stop even when it's past the height iFlusters 355 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

It is likely that Part.CFrame.Y doesn't become exactly 458.4. Many times when manipulating Vector3s or CFrames there can be rounding errors (very close numbers to what they should be, like 458.400002).

This kind of bug can usually be fixed by rounding the number and then comparing it.

function CheckYValue()
    while wait() do
        if math.floor(Part.CFrame.Y*10 + 0.5)/10 ~= 458.4 then -- rounding to the nearest 0.1
            Part.CFrame = Part.CFrame * CFrame.new(0, 0, 0.1)
        else
            return
        end
    end
end

Another way is to check if the difference between the two is very small.

function CheckYValue()
    while wait() do
        if math.abs(Part.CFrame.Y-458.4) > 0.01 then -- greater than 0.01 between the two
            Part.CFrame = Part.CFrame * CFrame.new(0, 0, 0.1)
        else
            return
        end
    end
end
Ad

Answer this question