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

Magnitude is only returning 0?

Okay so I am working on a detection function for my game. It is detecting if the player is within a certain distance of the plane so I can then play engine sounds.

local plane = workspace.PropellerPlane
local primaryPart = workspace.PropellerPlane:GetPrimaryPartCFrame()
local primaryPartVector3 = Vector3.new(primaryPart.p)


local playerCFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame
local playerVector3 = Vector3.new(playerCFrame.p)
local magnitudePrint = (primaryPartVector3 - playerVector3).Magnitude


while true do
	print (primaryPart.p)
	print(magnitudePrint)
	wait(.1)
end

I know this code is extremely messy but I am just trying to debug why it won't work.

When I have it print to the console the magnitudePrint returns 0 no matter what I do in game.

7 comments

0

Hmm.. this should work. You could try defining the Vector3 variables without using the Vector3.new functions, since '.p' returns a vector of the given CFrame anyways.

0

Does workspace.PropellerPlane have a PrimaryPart set?

0

No luck. Now it prints 2255.9067382812

0

The primary part is set in the following order- workspace/PropellerPlane(the model)/Fuselage/primaryPart

0

well uh is that ur distance from the propellers cx

0

No, the propellers are in a separate directory

0

I think I know what's happening. Send a message to the #scripting-questions channel in discord and I'll explain.

1 answer

✓ Accepted answer

You never change the value of magnitudePrint after line 8.

That means the same value will be output over and over and over by the while loop.

If you want to get the current distance, you have to recompute it in the while loop.

while true do
	wait(0.1)

	local playerCFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame
	local playerVector3 = Vector3.new(playerCFrame.p)
	local magnitudePrint = (primaryPartVector3 - playerVector3).Magnitude

    print (primaryPart.p)
    print(magnitudePrint)
end

playerVector3 is incorrect, however. playerCFrame.p is a Vector3 already. Don't give that to Vector3.new.

This is similarly a problem with primaryPartVector3. Don't pass a Vector3 to Vector3.new.

1 comment

0

It worked! Thank you so much!

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