Fall damage script not working?

I have a fall damage script, and when I test it, it doesn’t work unless I jump, but then it prints a negative value, like -1 (I have the Health CoreGui disabled, so I use print to get the damage).

-- server script
local module = require(game.ReplicatedStorage.GlobalFunctions)

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local startPos
		c.Humanoid.StateChanged:Connect(function(old, new)
			if new == Enum.HumanoidStateType.Freefall then
				startPos = c.HumanoidRootPart.Position.Y
			elseif new == Enum.HumanoidStateType.Landed then
				if not startPos then return end
				local landPos = c.HumanoidRootPart.Position.Y
				if startPos > landPos then return end
				local fallDamage = module.RoundNumber((startPos - landPos) / 2)
				c.Humanoid:TakeDamage(fallDamage)
				print(fallDamage)
			end
		end)
	end)
end)

-- module script 
local module = {}

module.RoundNumber = function(number)
	local finalnumber
	local remainder = number % 1
	if remainder >= 0.5 then 
		finalnumber = math.ceil(number) -- round up
	end
	if remainder < 0.5 then
		finalnumber = math.floor(number) -- round down
	end
	return finalnumber
end

return module

Even without the module script, it does the same thing, just with decimals. Can anyone help me out?

startPos will always be higher than landPos since your fall position is higher than your landing position. This means it will always return early.

I am aware of the fact that startPos is higher than landPos, but what do you mean by that it will return early?

if startPos > landPos then return end

If you fall from 10 to 5, 10>5.

1 Like

Whoops, that was a mistake, meant <. Changed it, and it works.

1 Like