Help with fall damage

For whatever reason it gives me negative numbers when I fall or doesn’t detected that I was falling at all. If anybody knows how to fix this please let me know how

Thanks in advance to all who reply :slightly_smiling_face:

Here’s my code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)
		local Velocity = 0
		local Humanoid = character:WaitForChild("Humanoid")
		local Root = character:WaitForChild("HumanoidRootPart")
		
		local states = {
			[Enum.HumanoidStateType.Freefall] = function()
				Velocity = math.abs(Root.Position.Y)
			end,
			[Enum.HumanoidStateType.Landed] = function()
				Velocity = math.abs(Velocity - math.abs(Root.Position.Y))
				if math.ceil(Velocity) >= 20 then
					Humanoid:TakeDamage(Velocity)
					Velocity = 0
				end
			end
		}
		
		Humanoid.StateChanged:Connect(function(_, newState)
			
			local func = states[newState]
			if func then
				func()
			end
			
		end)
	end)
	
end)
1 Like

I decided to do more research and I was able to completely redo my script, for those of you who are also struggling with fall damage, here’s what worked for me:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)
		local Velocity = 0
		local Humanoid = character:WaitForChild("Humanoid")
		local Root = character:WaitForChild("HumanoidRootPart")
		
		Humanoid.FreeFalling:Connect(function(active)
			if active then
				
				if Velocity == 0 then
					Velocity = math.abs(Root.Position.Y)
				end
				
			else
				
				local currPos = math.abs(Root.Position.Y)
				local diff = (Velocity - currPos)
				
				local canDamage = diff >= 5 and Root.Position.Y < Velocity
				
				Velocity = 0
				
				if canDamage then
					Humanoid:TakeDamage(diff)
				end
				
			end
		end)
	end)
	
end)