This fall damage script works, but not all the time. What’s wrong with it?
local lastYPos = {}
function connectFallDamage(Char)
local Humanoid = Char:FindFirstChild("Humanoid")
local InAir = false
Humanoid.StateChanged:Connect(function(old, new)
print(old, new)
if new == Enum.HumanoidStateType.Freefall then
if lastYPos[Char] then return end
local Root = Char:FindFirstChild("HumanoidRootPart")
if Root then
lastYPos[Char] = Root.Position.Y
print(lastYPos[Char].." Falling")
end
elseif old == Enum.HumanoidStateType.Freefall and new == Enum.HumanoidStateType.Landed and lastYPos[Char] then
local Root = Char:FindFirstChild("HumanoidRootPart")
if Root then
if lastYPos[Char] > Root.Position.Y then
local Dmg = (lastYPos[Char] - Root.Position.Y)/2
local health = game.ServerStorage.CharacterHealths:FindFirstChild(Char.Name.."Health")
if health then
if health.Value - Dmg <= 0 then
health.Value = 1
Event:Fire(Char, 15, true)
else
health.Value = health.Value - Dmg
end
end
end
end
lastYPos[Char] = nil
end
end)
end