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)
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?
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).
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
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 !
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)