Hitbox collision delay

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Hi! So I was making a knockback system which was good. But it was on client side and bad so I decided to rescript it.

  2. What is the issue? Main problem is a collision delay. Im using GetPartsInParts() to check if there anything touched player. It works but with big delay. Here code.

local module = {}
local TweenService = game:GetService("TweenService")

function module.Knocback(Character,Speed,Velocity)
	local Humanoid = Character.Humanoid --Character Humanoid
	local HRP = Character.HumanoidRootPart --Character Humanoid root part
	local Casting = true --Creating value to check if we must ray cast
	local Floor = false --Creating value to check if we must fire floor ray cast
	
	local KnockVelocity = Instance.new("BodyVelocity") --Creating body velocity
	KnockVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	KnockVelocity.Velocity = HRP.CFrame.lookVector * -Speed + Vector3.new(0,Velocity,0)
	KnockVelocity.Parent = HRP
	
	delay(.1,function() --Changing body velocity
		KnockVelocity.MaxForce = Vector3.new(math.huge,100,math.huge)
		KnockVelocity.Velocity = HRP.CFrame.lookVector * -Speed
		Floor = true
	end)
	
	local KnockbackAnimation = Humanoid:LoadAnimation(script.Knockback)
	KnockbackAnimation:Play()
	
	local Hitbox = Instance.new("Part")
	Hitbox.Name = "KnockbackHitbox"
	Hitbox.Size = Vector3.new(4,4,4)
	Hitbox.CanCollide = false
	Hitbox.Anchored = false
	Hitbox.Transparency = .8
	Hitbox.Parent = Character

	local weld = Instance.new("Weld",Hitbox)
	weld.Part0 = Hitbox
	weld.Part1 = HRP
	weld.C0 = CFrame.new(0,1.2,0)
	
	wait(.2)
	
	spawn(function() --Spawn function start
		while Casting == true do --While true loop start
			print("ayo")
			local Params = OverlapParams.new() --Creating raycast params
			Params.FilterDescendantsInstances = {Character}
			Params.FilterType = Enum.RaycastFilterType.Blacklist
			
			local parts = workspace:GetPartsInPart(Hitbox,Params)
			
			for i,v in pairs(parts) do
				if v.CanTouch == true then
					Casting = false
					Hitbox:Destroy()
					KnockVelocity:Destroy()
					KnockbackAnimation:Stop()
				end
			end
			
			wait()
		end --While true loop end
	end) --Spawn function end
end

return module

Here script in action.

  1. What solutions have you tried so far? I was using workspace:Raycast() and Ray.new() before. It had a delay too and so i looked up at dev forum to find bettter way to detect collision but delay is same. Is there any way to fix it?

Hello!

This is purely just because you are detecting collision on the server. I tried your code in a baseplate on mine and as it is instant on the client side, it is pretty delayed on the server side.

What I recommend is that you can detect when this knockbacks occur on the server, but perform the knockback effect on the client. If you’re not 100% safe with that, Some minor changes in the code helped with the delay.

  • Made the hitbox size on the Y axis a little bit bigger [4 → 5]
  • Replaced wait() with task.wait() considering it is a more optimal and improved way to yield

These still have a slight delay, but it is more than suitable for the server. This is purely just because of latency. You can also just stop the knockback when they hit the floor slightly sooner but it’s not recommended due to it’s potential inaccuracy. If it doesn’t hurt either you can still use the Touched event ROBLOX gives you. I hope this helped :smiley:

2 Likes

Thank you! Replaced loop on the client server, changed wait() function and changed hitbox size. Now everything works!