How do I fix this?

Hello, I’m having an issue where whenever I use sphere.CFrame = CFrame.lookAt(sphere.Position, waypoint.Position), the sphere just goes to the black zone in Roblox…

No Errors.

		repeat
			task.wait()
			sphere.CFrame = CFrame.lookAt(sphere.Position, waypoint.Position)
			
			local dPositionX = designatedPosition.X  -- designated
			local dPositionY = designatedPosition.Y
			local dPositionZ = designatedPosition.Z

			if cPositionX < dPositionX then
				sphere.Position += Vector3.new(.5, 0, 0)
			elseif cPositionX > dPositionX then
				sphere.Position -= Vector3.new(.5, 0, 0)
			end

			if cPositionY < dPositionY then
				sphere.Position += Vector3.new(0, .5, 0)
			elseif cPositionY > dPositionY then
				sphere.Position -= Vector3.new(0, .5, 0)
			end

			if cPositionZ < dPositionZ then
				sphere.Position += Vector3.new(0, 0, .5)
			elseif cPositionZ > dPositionZ then
				sphere.Position -= Vector3.new(0, 0, .5)
			end

			currentPosition = sphere.Position
			designatedPosition = waypoint.Position

			cPositionX = currentPosition.X
			cPositionY = currentPosition.Y
			cPositionZ = currentPosition.Z

			dPositionX = designatedPosition.X
			dPositionY = designatedPosition.Y
			dPositionZ = designatedPosition.Z
		until currentPosition == designatedPosition
1 Like

Try

sphere.CFrame = CFrame.new(sphere.Position, waypoint.Position)

instead of using CFrame.lookAt. I don’t know what you’re trying to do though.

1 Like

I’m trying to make a part look at another part.

while task.wait() do
	workspace.a.CFrame = CFrame.new(
		workspace.a.Position, -- a is the part that is looking
		workspace.o.Position -- o is the part the i move around
	)
end

This code worked for me.

Can you explain the difference from my script though?

Didn’t fix it

You were using CFrame.lookAt() in your script, whereas in mine I am using CFrame.new().
I am using CFrame.new() with two arguments: a Vector3 Position, and a Vector3 LookAt.

Can you send a place file so I can see?

(14) test - Roblox

Place file. So I can see the all of the code, as I assume the original post is not all of it. Also,

It is important to note here that using the constructor version of CFrame.new instead of CFrame.lookAt is deprecated. Note that CFrame.lookAt without a third argument behaves identically to CFrame.new, so thats not the issue.

For more information on why you should use CFrame.lookAt (other than it being more readable), see the article here:

1 Like

Sorry, used to using CFrame.new()

So do you think that the issue relates to me not setting a third argument?

I think your main issue here is trying to perfectly reach the waypoint by units of .5, which may end up never matching up due to floating point issues (.499999 does not equal .5). Try checking the distance between the two like this:

local distance = (designatedPosition - currentPosition).magnitude

if distance < 5 then
    -- Within 5 studs, either set directly or stop here
end

No, the third argument in CFrame.lookAt determines which direction is up, which has a default of Vector3.new(0,1,0). Unless it’s upside down, you don’t need to worry about it. Look at the docs for more info.

CFrame.new(a, b) == CFrame.lookAt(a, b)

Bit confusing, because .5 + .5 = 1, so how did it happen?

Floating point errors.

Floating point error in Python - GeeksforGeeks.

1 Like