LinearVelocity or Debris causing delay

  1. I do not know why there is a delay/lag at the end of knock back effect. I think it happens when Debris destroys an item after the duration ended. However, after few knockbacks everything works fine. Do I have to preload this and how would I do it

  2. This is when I just join the server. https://gyazo.com/46c92a0073767a6805931d8f680cc4fe
    this is after i knock npc for a few times.https://gyazo.com/f8994954e294a9e8ce8cc6bcd5a4e014

  3. I tried switching back to BodyVelocity, but that did not help.

		if activationCount == 2 then
			warn("KNOCK BACKKK")
			-- Apply knockback effect for the third activation
			-- Implement your knockback logic here
			local attachment = Instance.new("Attachment") --// Creating an attachment for the LinearVelocity (THIS IS IMPORTANT!)
			attachment.Name = "VelocityAttachment" --// Naming it
			attachment.Parent = hitHumanoid.Parent.HumanoidRootPart --// Parenting it
			
			local velocity = Instance.new("LinearVelocity") --// Creating the LinearVelocity
			velocity.Name = "Knockback" --// Naming it
			velocity.Attachment0 = attachment 
			velocity.Parent = hitHumanoid.Parent.HumanoidRootPart --// Parenting it to the HumanoidRootPart
			velocity.MaxForce = 1000000 --// Setting the max force (the most amount of force that will move the character)
			velocity.VectorVelocity = character.HumanoidRootPart.CFrame.LookVector * 30 --// Setting the vector velocity (the velocity direction it will move in)
			velocity.Enabled = true --// Enabling it (not necessary. It is enabled by default. I always like to make sure lol)
			
			game.Debris:AddItem(attachment, 0.7) --// Deletes the attachment after 0.7 seconds
			game.Debris:AddItem(velocity, 0.7) --// Deletes the velocity after 0.7 seconds
		end
1 Like

The delay or lag at the end of the knockback effect might be caused by the destruction of the attachment and velocity objects by the Debris service. When an object is destroyed by Debris, there might be a slight delay before it is actually removed from the game.

-- Preload the attachment and velocity objects
local preloadedObjects = {}

local function preloadObjects()
    local attachment = Instance.new("Attachment")
    attachment.Name = "VelocityAttachment"
    
    local velocity = Instance.new("LinearVelocity")
    velocity.Name = "Knockback"
    velocity.Attachment0 = attachment
    velocity.MaxForce = 1000000
    
    preloadedObjects.attachment = attachment
    preloadedObjects.velocity = velocity

end

preloadObjects()

-- Apply knockback effect
if activationCount == 2 then
    warn("KNOCK BACKKK")
    
    local attachment = preloadedObjects.attachment:Clone()
    attachment.Parent = hitHumanoid.Parent.HumanoidRootPart
    
    local velocity = preloadedObjects.velocity:Clone()
    velocity.Parent = hitHumanoid.Parent.HumanoidRootPart
    velocity.VectorVelocity = character.HumanoidRootPart.CFrame.LookVector * 30
    velocity.Enabled = true
    
    game.Debris:AddItem(attachment, 0.7)
    game.Debris:AddItem(velocity, 0.7)
end```
1 Like