# How to do fall damage in the best way?

Hello! I want to achieve the best fall damage. I’ve heard that you can do this using getState. But it didn’t work out for me, I don’t really know how to do calculations, I have a script that removes bounce when falling. Perhaps it will be possible to add fall damage to this script?

``````local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local primaryPart = character:WaitForChild("HumanoidRootPart")

humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed then
local velo = primaryPart.AssemblyLinearVelocity
primaryPart.AssemblyLinearVelocity = velo*Vector3.new(1,0,1)

local ray = Ray.new(primaryPart.Position, Vector3.new(0,-20,0))
local hit, pos = game.Workspace:FindPartOnRay(ray, character)

local hipHeight = humanoid.HipHeight
local newPos = pos + Vector3.new(0,hipHeight,0)
character:MoveTo(primaryPart.Position - primaryPart.Position + newPos)
end
end)
``````

Thanks anyway!

4 Likes

I would just create a new script all together to prevent confusion

If you want to create fall damage, you can use tick()
Or just use an if statement

2 Likes

But tick gives crazy numbers. How do I calculate that when falling, this number decreases and becomes not so large?

Tick allows you to calculate the time between 2 points in a script

``````local timers = tick()

wait(5)

Print(tick()-timers)
``````

Output =5

1 Like

Hm. I’ll try and see what happens this time.

More precisely, I will try to write a script, if I fall, the tick will start counting how many seconds I fly and how much damage I will receive.
Is it worth using seconds?

You can try this:

``````local TimeFalling = 0

RunService.Heartbeat:Connect(function(Delta)
if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
TimeFalling += Delta
else
if TimeFalling > 0.6 then
Humanoid:TakeDamage(TimeFalling * 10)
end

TimeFalling = 0
end
end)
``````

It counts how long the player is in the air, and if they’ve been in the air longer than a certain threshold (aka how long they need to fall before they can start taking damage, `0.6` in this case), it gives damage when they land (`TimeFalling * 10`).

3 Likes

The Landed humanoid state actually happens before the velocity of the character is removed when touching the ground. You could check how fast they were falling and apply damage accordingly

1 Like

couldnt you use

``````humanoid.MoveDirection
``````

? It gives a vector 3 on the direction the character is going,
you can check the Y proprety and see if it goes above a threshold.

``````humanoid.MoveDirection.Y
if humanoid.MoveDirection.Y > 15 then DamageTheDoO_O() end
``````

then wait for the player to stop falling, perhaps using

``````humanoid.StateType
if humanoid.MoveDirection.Y > 15 then humanoid.StateChanged:Wait() DamageTheDoO_O() end
``````

then do the damage.

1 Like

You can also calculate it in relation to the distance of the fall, if you wish!

Have a good day !

1 Like

This is work’s pretty good. But if Humanoid.Sit = true you don’t take fall damage

I’ve already try to get fall damage with the fall time but it’s not the best way because some times the player get stuck in the air so he get more damage and if the player jump all of the time he get more damage too, that’s what I think, have a nice day !

1 Like

I’ve update the code !

``````local RunService = game:GetService("RunService")
local Character = game.Players.LocalPlayer.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Y
local YF
local YL

RunService.RenderStepped:Connect(function()
Y = Character:GetPrimaryPartCFrame().Position.Y
end)

Humanoid.StateChanged:Connect(function(OldState, NewState)
if NewState == Enum.HumanoidStateType.Freefall then
YF = Y
elseif NewState == Enum.HumanoidStateType.Landed then
YL = Y
if YF - YL > 25 then
Humanoid:TakeDamage(YF - YL)
end
elseif OldState == Enum.HumanoidStateType.Freefall and NewState == Enum.HumanoidStateType.Swimming then
YL = Y
if YF - YL > 100 then
Humanoid:TakeDamage(Humanoid.MaxHealth)
end
end
end)
``````

Have a nice day !

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.