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

[ANSWERED] How do I get the Y position of the player's head when falling and landing?

Asked by 7 years ago
Edited 7 years ago

EDIT: Thanks to the people who helped me get out of a problematic -and weird- situation! Thank you very much!

This is the script I'm using now:

local head = game.Players.LocalPlayer.Character:WaitForChild("Head", 60)

local startPos = nil
local endPos = nil

local falling = false


humanoid.Jumping:connect(function(active)
    if active == true then
        startPos = (head.Position.Y)
        falling = true

        while falling == true do
            if head.Position.Y > startPos then
                startPos = (head.Position.Y)
            end
            wait()
        end

    elseif active ~= true then
        falling = false
        endPos = (head.Position.Y)
    end
end)

humanoid.StateChanged:connect(function(state)
    if state == Enum.HumanoidStateType.Landed then
        falling = false
        endPos = (head.Position.Y)
    end
end)

So, what i want is: I'm trying to get the Y position of the player's head when falling, then the head's Y position when landing, so I get the distance fallen.

So, imagine the player is standing on a high plataform, the player's head Y position when falling is 17, then the player's head Y position when landing on the baseplate is 4.5.

For some reason, The positions I get makes NO sense at all.

(NOTE: The following script is a LocalScript in StarterCharacterScripts. There will be another example after the following.)

Here's the example (with notes):

local head = game.Players.LocalPlayer.Character:WaitForChild("Head", 60)

local startPos = nil --The position I'll get when the player begins to fall.
local endPos = nil --The position I'll get when the player lands.
local fallingDistance = nil --I'll do stuff to this later.


humanoid.StateChanged:connect(function(state)
    if state == Enum.HumanoidStateType.Freefall then --When the player begins to fall:

        startPos = (head.CFrame.y) -- I'll get the falling position. (I tried head.Vector3.Y too...)
        print(startPos) -- For some reason THIS says the position of the head when falling is at ~4.3?

    elseif state == Enum.HumanoidStateType.Landed then --When the player lands:

        endPos = (head.CFrame.y) -- I'll get the landing position.
        print(endPos) -- THIS says that the position of the head is at ~4.2?

        fallingDistance = (startPos - endPos)
        print(fallingDistance) -- I often get NEGATIVE NUMBERS, so THIS is telling me the player is falling upwards???

    end
end)

Here's another example (without notes): (Keep reading, still stuff to clear down below.)

local head = game.Players.LocalPlayer.Character:WaitForChild("Head", 60)

local startPos = nil
local endPos = nil
local fallingDistance = nil

humanoid.StateChanged:connect(function(state)
    if state == Enum.HumanoidStateType.Freefall then

        startPos = (head.CFrame.y) -- Gets ~4.3 when FALLING.
        print(startPos)

    elseif state == Enum.HumanoidStateType.Landed then

        endPos = (head.CFrame.y) -- Gets ~4.2 when LANDING.
        print(endPos)

        fallingDistance = (startPos - endPos)
        print(fallingDistance) -- Sometimes this gives me negative numbers...

    end
end)

If the head's position when falling from the plataform was 4.3, your feet would've been stuck in the baseplate, and the player would've been inside the plataform. (by logic.)

Hope someone figures this out... Have a nice day / night!

0
The StateChanged function for Freefall is called about a split second before when it's called for Landed, but there is an event in the humanoid called FreeFalling, so you could use that. UgOsMiLy 1074 — 7y
0
There is no Enum. that says "FreeFalling", there's just "Freefall" SuperAndresZ_YT 202 — 7y
0
It's an event. Humanoid.FreeFalling. Eqicness 255 — 7y

2 answers

Log in to vote
0
Answered by
Eqicness 255 Moderation Voter
7 years ago

Hello!

You did everything right, you just forgot one critical thing: A CFrame is made of two Vector3s: A position and a lookVector. When you reference CFrame.y, you're referencing the y value of both the Position and Look Vector. To only reference the position, add a .p in between the CFrame and .y. So, your Y position will look like:

startPos = head.CFrame.p.y

Have an awesome time coding ;D

0
Dang, Thanks! Imma test that out! SuperAndresZ_YT 202 — 7y
0
That is good advice, but putting CFrame.y would still print the same as CFrame.p.y UgOsMiLy 1074 — 7y
0
I don't believe it would. @UgOsMiLy Eqicness 255 — 7y
Ad
Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
7 years ago
Edited 7 years ago

Here is what I think you should try.

local head = game.Players.LocalPlayer.Character:WaitForChild("Head", 60)

local startPos = 0
local endPos = 0
local fallingDistance = 0

humanoid.FreeFalling:Connect(function(active)
    if active == true then
        startPos = (head.CFrame.y)
        print(startPos)
    else
        endPos = (head.CFrame.y)
        print(endPos)

        fallingDistance = (startPos - endPos)
        print(fallingDistance)
    end
end)
0
You should put a .p in between the head.CFrame and .y, it's better formatting and more exact. Eqicness 255 — 7y

Answer this question