Velocity Based fall damage script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I would like to achieve velocity based fall damage.

  1. What is the issue? Include screenshots / videos if possible!

It is not working and I don’t know why.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, I have looked for soulutions on the DevForums.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local damageVelocity = 16

game:GetService("Players").PlayerAdded:Connect(function (plr)
	plr.CharacterAdded:Connect(function (char)

		local root = char:WaitForChild("LowerTorso")
		local humanoid = char:WaitForChild("Humanoid")

		if humanoid and root then

			humanoid.StateChanged:Connect(function (oldState, newState)
				if humanoid:GetState() == Enum.HumanoidStateType.Landed then
					print(root.Velocity.Magnitude)
					if root.Velocity.Magnitude >= damageVelocity then
						humanoid.Health = humanoid.Health - (root.Velocity.Magnitude)
					end
				end
			end)
		end
	end)
end) 

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

What exactly isn’t working? Are there any errors? Have you tried print debugging it yet?

EDIT: The first thing I see is you’re trying to get the Velocity of the LowerTorso after the character has already landed. You need to get it before it’s landed

Using basic scripting logic, the fault exists within your code. You’re checking if the velocity of the player’s primary part is higher AFTER it has landed. Also, what you’re looking for is FreeFalling, not FallingDown, both act different.

Relevant: HumanoidStateType | Documentation - Roblox Creator Hub

What you can do is capture the velocity while the player is falling and then deal damage. You may also want to do this in a new thread because you may want to do other things while the script works on velocity based damage.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local WAIT_FOR_CHILD_TIME_OUT = 60
local LOWEST_VELOCITY = 16 * 2.5
local FALL_DAMAGE_DIVIDER = 2.5

local function PlayerAdded(player)
	local function CharacterAdded(character)
		local humanoid = character:WaitForChild("Humanoid", WAIT_FOR_CHILD_TIME_OUT)
		local primaryPart = character:WaitForChild("HumanoidRootPart", WAIT_FOR_CHILD_TIME_OUT)

		if not (humanoid and primaryPart) then
			return
		end

		local fallSpeed = 0

		humanoid.StateChanged:Connect(function (oldState, newState)
			if newState == Enum.HumanoidStateType.Landed then
				humanoid.Health -= fallSpeed >= LOWEST_VELOCITY and fallSpeed / FALL_DAMAGE_DIVIDER or 0
			end
		end)
		
		while true do
			if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
				fallSpeed = character.PrimaryPart.Velocity.Magnitude 
			end

			RunService.Heartbeat:Wait()
		end
	end

	CharacterAdded(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(CharacterAdded)
end

Players.PlayerAdded:Connect(PlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end
1 Like