Magnitude does not print

Been a bit,

I have a script that won’t print a stud distance from my door. I’m unsure why it doesn’t give me any errors.

SCRIPT:

--PLAYER VARIABLES
local PLR = game.Players.PlayerAdded:Wait()
local CHR = PLR.Character or PLR.CharacterAdded:Wait()

--PART VARIABLES
local PrmPart = script.Parent.Carpet -- The carpet is the PrimaryPart, or the PartVector

--VECTOR VARIABLES
local PLRVector = Vector3.new(CHR:WaitForChild("HumanoidRootPart").Position) --Player's Vector3
local PARTVector = Vector3.new(PrmPart.Position)

--MAGNITUDES
local Mag = (PLRVector - PARTVector).Magnitude -- Magnitude

--CODE
while true do
	wait(0.5)
	print(Mag .. " studs from the door.") --Prints the magnitude distance
end

WORKSPACE:
image

Explanations would be great, I’m still rusty.

Since you have already added this line:

Just remove the :WaitForChild()
The character has already loaded at this point

2 Likes

I’ve actually tried this before, it gave me the same result. Always prints 0.

Ok, then switch these two around: PARTVector - PLRVector

Why are you trying to create a Vector3 out of a Vector3?, Position is already a Vector3?

Just do:

local PLRVector = CHR:WaitForChild("HumanoidRootPart").Position
local PARTVector = PrmPart.Position
1 Like

That is very true, good thinking @DasKairo

1 Like

One problem is that you are never going to get a different number because you are only calculating once, so every print will say the same thing. You will have to put the magnitude calculation and re-get the positions in the while true do in order for anything to change.

3 Likes

You are putting the position of the part inside of a vector3, when the position is already defined as one. You can turn

local PLRVector = Vector3.new(CHR:WaitForChild("HumanoidRootPart").Position) --Player's Vector3
local PARTVector = Vector3.new(PrmPart.Position)

into this

local PLRVector = CHR:WaitForChild("HumanoidRootPart").Position --Player's Vector3
local PARTVector = PrmPart.Position
1 Like

so how are you saying I fix my while true do loop?

Saw somebody else do it, thought it would help.

What is the meaning of this? Why is this here? What are you even trying to achieve?

1 Like

just write this part maybe this, but i´m unsure bc i don´t have that much experience. if not then i´m sorry couldn´t help.

–PLAYER VARIABLES
local PLR = game.Players.PlayerAdded:Wait()

1 Like

Alright, make a script in StarterCharacterScripts and put the following code:

local char = script.Parent

local carpet = workspace:WaitForChild("Door"):WaitForChild("Carpet")

game:GetService("RunService").Heartbeat:Connect(function()
    local Mag = (char:FindFirstChild("HumanoidRootPart").Position - carpet.Position).Magnitude
    print(Mag .. " studs from the door.")
end)

The issue with your script is the way you find the players character, and you don’t ever update the variables with new information, but my script fixes that issue.

Also while true loops with waits are bad. A heartbeat loop runs every frame and has no delay.

local Players = game:GetService("Players");

local PrmPart = script.Parent:WaitForChild("Carpet");

local function on_char_added(character)
    while character.PrimaryPart.Parent do
        local dist = (character.PrimaryPart.Position - PrmPart.Position).Magnitude;
        print(dist .. " studs from the door.");
        task.wait(0.5);
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(on_char_added);
end)

Above code should suit your needs. To detect new players, use Players.PlayerAdded event, similarly use player.CharacterAdded for detecting character being added. Run a while loop while the character exists, calculating the magnitude every 0.5 seconds (using task.wait(), as it is more accurate).

Just use a starter character script like I posted, it’s better for this situation

To get the player, so I can get the character?

Thats already what I did. Did you make changes to it that im not seeing?

It works. The only issue is that you forgot to put .Position for the Character primary part. Thanks anyway, I have to start using functions a lot more.

Apologies. Updated my solution. Have fun! :^)

1 Like