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.
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
.