Player receives knockback a second later after being punched

Hello, I have been clueless since my script works the way I want it do work but it lags a little bit when applying knockback to another player, but when I get hit there is no lag? I have been trying to fix this since please help me!!

local function applyKnockback(character, plr, speed, upwardForce)
    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    local enemyPlayer = game.Players:GetPlayerFromCharacter(character)

    if humanoidRootPart and enemyPlayer then
        local isBlocking = enemyPlayer:FindFirstChild("isBlocking") and enemyPlayer.isBlocking.Value

        if isBlocking then
            speed = speed * 0.2 
            upwardForce = Vector3.new(0, 0, 0) 
        else
            ragdollModule.EnableRagdoll(character)
        end

        local knockbackDirection = (humanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).Unit
        local combinedForce = (knockbackDirection + upwardForce).Unit

        enemyPlayer:FindFirstChild("LastHit").Value = plr.Name

        local bodyVelocity = Instance.new("BodyVelocity")
        bodyVelocity.MaxForce = Vector3.new(9999, 9999, 9999)
        bodyVelocity.Velocity = combinedForce * speed
        bodyVelocity.Parent = humanoidRootPart
        electricityEffect.Parent = humanoidRootPart

        task.wait(0.1)
        bodyVelocity:Destroy()


        if not isBlocking then
            Movement.disableMovement(character:FindFirstChild("Humanoid"))
            task.wait(1.5)
            ragdollModule.DisableRagdoll(character)
            Movement.enableMovement(character:FindFirstChild("Humanoid"))
        end
    end
end

Imgur: The magic of the Internet (How I want it to be)
Imgur: The magic of the Internet (How mine is)

Imgur: The magic of the Internet (a better showcase of the problem look 1 player first then the other)

1 Like

I think this may just be an issue of a delay between clients. In this gif :

You can see from Player 2’s POV there is a delay, while there is no delay from Player 1’s POV.

A possible solution could be somehow locally simulating Player 1’s ragdoll trajectory after being punched for Player 2’s POV.

The lag probably comes from doing the knock back on the server or your computer. Basically replication delay causes this lag

1 Like