Number becoming NAN for some reason?

I’m trying to get an object to move around in an area, and I also want it to point in that direction. However, when I try to tell it to that point, in that direction, it says that it is “NAN” (not a number) and puts it all the way at the 128-bit integer limit! What am I doing wrong here, and how can I fix this?

local twnsrv = game:GetService("TweenService")
local point0 = workspace.area.point0.Position
local point1 = workspace.area.point1.Position

while true do
	local pos = script.Parent["Meshes/hoopfish"].Position
	local newpos = Vector3.new(
		math.random(point1.X, point0.X),
		math.random(point1.Y, point0.Y),
		math.random(point1.Z, point0.Z)
	)
	local distance = (pos-newpos).Magnitude
	local twninfo = TweenInfo.new(distance/3, Enum.EasingStyle.Linear)
	local cfra = CFrame.new(newpos, newpos)
	local twn = twnsrv:Create(script.Parent.PrimaryPart, twninfo, {CFrame = cfra})
	print(cfra)
	script.Parent.PrimaryPart.CFrame = CFrame.lookAt(pos, newpos) -- so its pointing in that direction from the very beginning
	twn:Play()
	print("eee")
	twn.Completed:Wait()
end

This is the output of the print(cfra)

1 Like

You’re creating a CFrame at a position that also looks at that position, which isn’t possible. Did you mean to use lookAt or lookAlong instead?

1 Like

Turns out I ended up fixing it myself lol. For anyone wondering, I put the lookAt line before defining cfra, and cfra became the position of the primary part * some CFrame.Angles thing. (also had to convert degrees to radians)

(also distance/25 is just a testing value so I can see it more quickly. It’s just a value that makes sure that the fish moves at a consistent speed no matter the distance needed to travel : ) )

local twnsrv = game:GetService("TweenService")
local point0 = workspace.area.point0.Position
local point1 = workspace.area.point1.Position

while true do
	local pos = script.Parent["Meshes/hoopfish"].Position
	local newpos = Vector3.new(
		math.random(point1.X, point0.X),
		math.random(point1.Y, point0.Y),
		math.random(point1.Z, point0.Z)
	)
	local distance = (pos-newpos).Magnitude
	
	local twninfo = TweenInfo.new(distance/25, Enum.EasingStyle.Linear)
	script.Parent.PrimaryPart.CFrame = CFrame.lookAt(pos, newpos)
	
	local prim = script.Parent.PrimaryPart
	local cfra = CFrame.new(newpos) * CFrame.Angles(
		prim.Orientation.X*math.pi/180,
		prim.Orientation.Y*math.pi/180,
		prim.Orientation.Z*math.pi/180
	)

	local twn = twnsrv:Create(script.Parent.PrimaryPart, twninfo, {CFrame = cfra})
	print(cfra)
	twn:Play()
	print("eee")
	twn.Completed:Wait()
end
1 Like

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