Falling Damage System issue

hello, i writed my own fall damage system but it’s so laggy and sometimes it doesn’t register damage when i falling - how i can fix this code?

print("Currently Fixed")

I will be very glad if someone tries to help me :slight_smile:

3 Likes

I forgot to say, I don’t use the changed state because fall damage with ragdoll

1 Like

This should really help with your lag issue


local character = script.Parent.Parent
local Blood = character.Blood
local fall = false
local maxFallVelocity = 0
local fallStartHeight = 0
local deb = false

-- Blood values dictionary
local bloodValues = {
    ["Left Arm"] = Blood.Vals.LA,
    ["Left Leg"] = Blood.Vals.LL,
    ["Right Arm"] = Blood.Vals.RA,
    ["Right Leg"] = Blood.Vals.RL,
    ["Head"] = Blood.Vals.H
}

local function getDamageColor(limbDmg)
    if limbDmg <= 0 then
        return Color3.new(0, 1, 0) -- Green
    elseif limbDmg >= 50 then
        return Color3.new(1, 0, 0) -- Red
    else
        local red = 0
        local green = 0
        local blue = 0

        if limbDmg < 30 then
            red = limbDmg / 30
            green = 1
        else
            red = 1
            green = 1 - ((limbDmg - 30) / 20)
        end

        return Color3.new(red, green, blue)
    end
end

local function takeDamage(velocity, limb)
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    local limbName = limb.Name
    local bloodValue = bloodValues[limbName]

    -- Damage is proportional to the velocity
    local damage = math.abs(math.floor(velocity)) * 2
    local color = getDamageColor(damage)

    -- Apply damage if it exceeds the threshold
    if damage > 0 then
        script.Parent.DamageLimb:FireServer(bloodValue, damage)
        script.Parent.Damage:FireServer(humanoid, damage)
    end

    -- Show damage effect with highlight
    local highlight = game.ReplicatedStorage.Highlight:Clone()
    game.Debris:AddItem(highlight, 0.5)
    highlight.FillColor = color
    highlight.OutlineColor = color
    highlight.Parent = limb
end

local function isGrounded()
    local rayOrigin = character.HumanoidRootPart.Position
    local rayDirection = Vector3.new(0, -30, 0) -- Distance to check downwards
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {character}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
    return raycastResult and raycastResult.Instance ~= nil
end

game["Run Service"].Heartbeat:Connect(function()
    -- Only track fall when character is in ragdoll state
    if character.Humanoid:FindFirstChild("RigType") and character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
        -- Track the fall only when the humanoid's root part is moving downwards at a significant speed
        local velocityY = character.HumanoidRootPart.Velocity.Y
        if not fall and velocityY < -20 then
            fall = true
            fallStartHeight = character.HumanoidRootPart.Position.Y
            maxFallVelocity = velocityY
            script.Parent.Falling:FireServer()
        end

        -- Monitor fall until the character hits the ground
        if fall then
            -- Track the fall velocity to calculate damage
            local currentVelocity = character.HumanoidRootPart.Velocity.Y
            if currentVelocity < maxFallVelocity then
                maxFallVelocity = currentVelocity -- Update max fall velocity if it gets worse
            end

            -- If the character reaches the ground, apply damage
            if isGrounded() then
                takeDamage(maxFallVelocity, character.HumanoidRootPart)
                maxFallVelocity = 0
                fall = false
            end
        end
    end
end)


From what I understand you said, you have a rag doll system.

1 Like

woah, you really fixed all issued with my fall-damage system - thanks so much!

1 Like