Why is this throwing an error

I’m getting an error on this line of code:

local startPosition = script.Parent.Handle.CFrame.p
local mag = (throwPart.CFrame.p-startPosition).Magnitude -- error line

ThrowPart is a regular part.


1 Like

maybe try this:

local mag = Vector3.new((throwPart.CFrame.p - startPosition).Magnitude)

I would say this will throw an error, magnitude is distance in studs, isn’t it?

I have to say I don’t really see where is it not working. There is a code I tested right now with two regular parts, and it worked just fine.

local gameing = game:GetService("Workspace"):WaitForChild("gameing")

local green = gameing:WaitForChild("green").CFrame
local red = gameing:WaitForChild("red").CFrame

local mag = (green.p - red.p).magnitude
print(tostring(mag)) --prints the distance; no error thrown

No idea why then, here’s the only line of code using the “mag” variable:

local ray = Ray.new(startPosition,(throwPart.CFrame.p - startPosition).unit * mag)
1 Like

And what line does throw an error? This, or the previous?

The previous line is erroring.

Maybe try using .magnitude non-capitalized? Don’t think that would work anyways. Don’t you have two same named variables?

Non-capitalized is throwing the same error, and I don’t have two same name variables… I think.

Here’s all the code involving the mag variable:

local startPosition = script.Parent.Handle.CFrame.p
		while not part do
			local mag = (throwPart.CFrame.p-startPosition).magnitude
			local ray = Ray.new(startPosition,(throwPart.CFrame.p - startPosition).unit * mag)

Well, I have to say I don’t know where it’s the error then. I’m not really familiar with the .unit tho.

I would like to throw in a friendly reminder that Cframe.p is deprecated and CFrame.Position is preferred. (Same goes for magnitude → Magnitude). Its not like there will be game breaking effects, it’s just a good habit to get into.

As for the problem, I feel more information is needed. What you claim to be the error line does not match with the error. If you could take a full picture of the error including the line it occurs on as well as the full script that would be helpful. What you have shown should not produce an error.

1 Like

Re-wrote the code, no errors appearing. Not sure what was wrong, but it’s fixed now.