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

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

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

4 comments

0

Could you give a bit more context of what you are trying to do with the code?

0

The if statement on line 3, if the Part Y Value isn't at 458.4 then it will run the code.

0

I get that but what is the code doing that you are not expecting it to?

0

It moves the part up constantly, as shown on line 4, it doesn't stop even when it's past the height

1 answer

✓ Accepted answer

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

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