Why the if statement won't work even the condition is met

so this is my script

while true do
	wait(0.1)
	if script.Parent.PrimaryPart.Position.Y == 11.1 then
		wait(0.1)
		script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame + Vector3.new(0,0.1,0))
	else
		if script.Parent.PrimaryPart.Position.Y == 11.7 then
			wait(0.1)
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame + Vector3.new(0,-0.1,0))
		end
	end
end

the primary part y axis position is 11.1 but still the if statement won’t work I tried to change it to CFrame.Position but still the same problem (There is no output errors the script just assume that the condition isn’t met

A float is very unlikely to land on those exact numbers, especially if you are adding 0.1 several times. You should use a “loose” check to avoid this problem:

local looseness = 0.01
if math.abs(script.Parent.PrimaryPart.Position.Y - 11.1) < looseness then
  ...

This happens because not every number is exactly representable as a float. You can use this calculator to see what actually gets stored when you enter 0.1 or 11.1 or 11.7: IEEE-754 Floating Point Converter

In your specific case it looks like you’re moving a platform up and then down, so you could use > and < instead of ==.

2 Likes