How to prevent knpckback from glitching player?

I have this knockback code and it works fine most of the time, but itsometimes glitches the player through walls/floors. How can I preven this from hapening?


local function doKnockback(root: BasePart, velocity: Vector3)

	local knockbackAtch = root:FindFirstChild("KNOCKBACK ATTACHMENT")

	if not knockbackAtch then
		knockbackAtch = Instance.new("Attachment")
		knockbackAtch.Name = "KNOCKBACK ATTACHMENT"
		knockbackAtch.Parent = root
	end

	local knockbackVelocity = root:FindFirstChild("KNOCKBACK VELOCITY")

	if not knockbackVelocity then
		knockbackVelocity = Instance.new("LinearVelocity")
		knockbackVelocity.Name = "KNOCKBACK VELOCITY"
		knockbackVelocity.Attachment0 = knockbackAtch
		knockbackVelocity.MaxForce = math.huge
		knockbackVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
		knockbackVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
		knockbackVelocity.Enabled = false
		knockbackVelocity.Parent = root
	end

	knockbackVelocity.VectorVelocity = velocity

	knockbackVelocity.Enabled = true
	task.wait(0.15)
	knockbackVelocity.Enabled = false
end


try making the MaxForce be not infinity cuz it gets wonky sometimes… Try like 1e9

2 Likes

thanks! I did that and i also added a cap to the vertical knockback so that it’s never negative (so players don’t get shoved into the ground and glitch under the map).

1 Like