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