Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Magnitude is only returning 0?

Asked by 7 years ago

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.

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. Goulstem 8144 — 7y
0
Does workspace.PropellerPlane have a PrimaryPart set? Goulstem 8144 — 7y
0
No luck. Now it prints 2255.9067382812 dude7054 10 — 7y
0
The primary part is set in the following order- workspace/PropellerPlane(the model)/Fuselage/primaryPart dude7054 10 — 7y
View all comments (3 more)
0
well uh is that ur distance from the propellers cx Goulstem 8144 — 7y
0
No, the propellers are in a separate directory dude7054 10 — 7y
0
I think I know what's happening. Send a message to the #scripting-questions channel in discord and I'll explain. GoldenPhysics 474 — 7y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

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.

0
It worked! Thank you so much! dude7054 10 — 7y
Ad

Answer this question