Knockback and Ragdoll Bug

When players touch the hitbox in the game, they are supposed to get knocked back slightly and then go into a ragdoll state. However, there’s an issue where the knockback doesn’t happen immediately. Instead, there is a delay of about 2-3 seconds before the ragdoll effect kicks in. Once it does, the players are flung away with a lot of force, causing them to fly far distances. I don’t know how to fix this if anyone can help adjust the script so that the knock back happens instantly when the hit box is touched, followed immediately by the rag doll effect, creating a smoother and more predictable reaction. Below is the script that makes them get knock back and rag dolled.

local function applyKnockbackAndRagdoll(enemyHum, direction)
    local enemyHumRp = enemyHum.Parent:FindFirstChild("HumanoidRootPart")
    if enemyHumRp then
        local force = Instance.new("BodyVelocity", enemyHumRp)
        force.MaxForce = Vector3.new(1, 1, 1) * math.huge
        force.Velocity = direction * knockbackForce
        debris:AddItem(force, 0.35)

        local enemyChar = enemyHum.Parent
        local ragdoll = RagdollModule.new(enemyChar)
        ragdoll:Ragdoll()

        task.delay(3, function()
            ragdoll:UnRagdoll()
        end)
    end
end

local function createHitbox(vfxClone)
    local hitbox = Instance.new("Part")
    hitbox.Size = Vector3.new(10, 10, 10)
    hitbox.Anchored = true
    hitbox.CanCollide = false
    hitbox.Transparency = 1
    hitbox.Color = Color3.new(1, 0, 0)
    hitbox.Name = "Hitbox"
    hitbox.CFrame = vfxClone.CFrame
    hitbox.Parent = vfxClone
    return hitbox
end

local function attachVFXToPlayer(player)
    local character = player.Character
    if not character then
        warn("Character not found for player: " .. player.Name)
        return
    end

    local vfxModel = insertService:LoadAsset(VFXModelID)
    local vfxPart = vfxModel:FindFirstChildWhichIsA("Part")
    if not vfxPart then
        warn("VFX part not found in the model!")
        return
    end

    local vfxClone = vfxPart:Clone()
    vfxClone.Anchored = false
    vfxClone.CanCollide = true
    vfxClone.Transparency = 1
    vfxClone.Name = "VFXClone"

    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    if humanoidRootPart then
        vfxClone.Parent = character
        vfxClone.CFrame = humanoidRootPart.CFrame

        local weld = Instance.new("WeldConstraint")
        weld.Part0 = vfxClone
        weld.Part1 = humanoidRootPart
        weld.Parent = vfxClone

        local hitbox = createHitbox(vfxClone)

        local highlight = Instance.new("Highlight")
        highlight.Adornee = vfxClone
        highlight.FillColor = Color3.new(1, 1, 0)
        highlight.FillTransparency = 0.5
        highlight.OutlineTransparency = 1
        highlight.Parent = vfxClone

        local emitters = getAllParticleEmitters(vfxClone)

        disableControlsAndMovement(player)

        delay(1.49, function()
            for _, emitter in pairs(emitters) do
                emitter.Enabled = true
            end

            hitbox.Touched:Connect(function(hit)
                if hit:IsA("BasePart") and hit.Parent ~= character then
                    local enemyHum = hit.Parent:FindFirstChildOfClass("Humanoid")
                    if enemyHum then
                        local enemyChar = enemyHum.Parent
                        local enemyHumRp = enemyChar:FindFirstChild("HumanoidRootPart")
                        if enemyHumRp then
                            local direction = (enemyHumRp.Position - hitbox.Position).Unit
                            applyKnockbackAndRagdoll(enemyHum, direction)
                        end
                    end
                end
            end)

            delay(4, function()
                vfxClone:Destroy()
                enableControlsAndMovement(player)
            end)
        end)
    else
        warn("HumanoidRootPart not found for player: " .. player.Name)
    end
end

Good thing i made a tutorial like 20 minutes back

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.