Fall damage script not working

No errors were shown, this is the script.

-- Objects
local settingsDir = script.Settings

function getSetting (name)
	return settingsDir and settingsDir:FindFirstChild(name) and settingsDir[name].Value
end

-- Variables
local damageHeight = getSetting("Damaging height") or 5 -- The height at which the player will start getting damaged at
local lethalHeight = getSetting("Lethal height") or 8 -- The height at which the player will get killed

game:GetService("Players").PlayerAdded:Connect(function (plr)
	plr.CharacterAdded:Connect(function (char)
		
		local root = char:WaitForChild("HumanoidRootPart")
		local humanoid = char:WaitForChild("Humanoid")
		
		if humanoid and root then
			
			local headHeight
			
			wait(3) -- Prevent the player from dying on spawn
			humanoid.FreeFalling:Connect(function (state)
				if state then
					headHeight = root.Position.Y
				elseif not state and headHeight ~= nil then
					pcall(function ()
						
						local fell = headHeight - root.Position.Y
						
						if fell >= lethalHeight then
							humanoid.Health = 0
						elseif fell >= damageHeight then
							humanoid.Health -= 10
						end
					end)
				end
			end)
		end
	end)
end)
2 Likes

This is the fall damage script I use for my game. It’s much more organized than that classic free one. Also, idk why it wouldn’t be working, but this script will perform much better.

local damageHeight = 14

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local debounce = false

		local humPart = char:WaitForChild("HumanoidRootPart")
		local humanoid = char:WaitForChild("Humanoid")

		if humanoid and humPart then
			local headHeight
			task.wait(3)

			humanoid.FreeFalling:Connect(function(state)
				if state then
					headHeight = humPart.Position.Y
				elseif not state and headHeight ~= nil and not char:FindFirstChild("CancelFall") and humanoid:GetState() ~= Enum.HumanoidStateType.Climbing and not debounce then
					debounce = true

					local fell = (headHeight - humPart.Position.Y)
					if (fell >= damageHeight) and game.ReplicatedStorage.Values.Game.Value == true then
                                              humanoid.Health -= fell*1.3
					      task.wait(0.5)
					     debounce = false
                                       end
				end
			end)
		end
	end)
end)

Would I put this in starter character scripts?

1 Like

No, server script service, because it listens for players to join. You wouldn’t want a player join listener in a client script.

Edited some things on your script to debug since it didn’t initially work. Does not print the 4th, don’t know why.

local damageHeight = 5
print("works1")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local debounce = false
		print("works1.5")
		local humPart = char:WaitForChild("HumanoidRootPart")
		local humanoid = char:WaitForChild("Humanoid")

		if humanoid and humPart then
			local headHeight
			task.wait(3)
			print("works2")

			humanoid.FreeFalling:Connect(function(state)
				if state then
					headHeight = humPart.Position.Y
				elseif not state and headHeight ~= nil and not char:FindFirstChild("CancelFall") and humanoid:GetState() ~= Enum.HumanoidStateType.Climbing and not debounce then
					debounce = true
					print("works3")

					local fell = (headHeight - humPart.Position.Y)
					if (fell >= damageHeight) and game.ReplicatedStorage.Values.Game.Value == true then
						humanoid.Health -= fell*10
						task.wait(0.5)
						print("works4")
						debounce = false
					end
				end
			end)
		end
	end)
end)

By the way, I added a value in replicated storage just in case that was the issue.