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?