How to fix hitbox latency on a multi-hitbox system

I made a multi-hitbox system for my game and i’m trying to fix the hitbox latency. I’ve seen things on youtube that say how to fix latency and they are simple and work, but a thing they all share is that they all are using a single hitbox. My system creates a bunch of hitboxes during the attack animation for more accurate hitboxes if the person were to turn around or something along those lines.

Here’s the code I’m using for the hitbox system (dont get scared off only like 5 lines are important)

client:

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Remotes = RS:WaitForChild("Remotes")
local m1Event = Remotes:WaitForChild("M1Event")

local tool = script.Parent
local originalName = tool.Name

local animation = tool:WaitForChild("AttackAnimation")

local equipped = false
local db = false

local cd = 2
local damage = 10
local size = Vector3.new(5,5,5)
local offset = Vector3.new(0, 0, -3.5)
local startup = 0
local Time = 1
local increment = 0.05

local function coolDown(duration)
	duration *= 10
	for i = duration, 1, -1 do
		if (i/10) % 1 == 0 then
			tool.Name = tostring(i / 10).. ".0" -- int
		else
			tool.Name = tostring(i / 10) -- float
		end
		task.wait(0.1)
	end
	tool.Name = originalName
end

UIS.InputBegan:Connect(function(input, gpe)
	if gpe or db or not equipped then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("input detected")
		db = true
		m1Event:FireServer(damage, size, offset, startup, Time, increment, animation)
		coolDown(cd)
		db = false	
	end
end)

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

server:

local RS = game:GetService("ReplicatedStorage")
local Remotes = RS:WaitForChild("Remotes")
local m1Event = Remotes:WaitForChild("M1Event")

local function createHitbox(damage, size, offset, hrp, hitboxes, touchedTable)
	local hitbox = Instance.new("Part")
	hitbox.CanCollide = false
	hitbox.Transparency = 0.75
	hitbox.Size = size
	hitbox.Parent = workspace.Game.Debris
	hitbox.CFrame = hrp.CFrame * CFrame.new(offset)
	hitbox.Anchored = true
	
	table.insert(hitboxes, hitbox)
	
	local touchedParts = workspace:GetPartBoundsInBox(hitbox.CFrame, hitbox.Size)
	for i, part in touchedParts do
		table.insert(touchedTable, part)
	end
end

m1Event.OnServerEvent:Connect(function(player, damage, size, offset, startup, Time, increment, animation)
	local character = player.Character or player.CharacterAdded:Wait()
	local hrp = character:WaitForChild("HumanoidRootPart")
	local humanoid = character:WaitForChild("Humanoid")
	
	if animation then
		local animator =  humanoid:WaitForChild("Animator")
		local aniamtionTrack = humanoid:LoadAnimation(animation)
		
		aniamtionTrack:Play()
	end
	
	task.wait(startup) -- startup time
	
	local hitboxes = {}
	local touchedParts = {}
	local damagedPlayers = {}
	
	local breakLoop = false
	
	for i = 1, math.floor(Time / increment), 1 do
		createHitbox(damage, size, offset, hrp, hitboxes, touchedParts)
		
		for i, part in touchedParts do
			if table.find(hitboxes, hitboxes[part]) or table.find(damagedPlayers, damagedPlayers[part.Parent.Name]) or not part.Parent:FindFirstChild("Humanoid") then 
				table.remove(touchedParts, touchedParts[part]) -- removes hitboxes, non-charcater objects, and anything already damaged
			else
				if part.Name == "playerHitbox" and part.Parent.Name ~= character.Name then
					table.insert(damagedPlayers, part.Parent.Name)
					part.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
					breakLoop = true -- once hit it will stop casting the hitboxes
				end
			end
		end
		
		table.clear(touchedParts)
		
		task.wait(increment)
		
		if breakLoop then break end
	end
	
	for i, hitbox in hitboxes do
		hitbox:Destroy()
	end
end)

If anybody can find out how to fix latency with this, please tell me as i’m kinda stumped. Any and all help is greatly appreciated and thanks in advance if you try to tackle this issue! :smile:

1 Like

The latency is because you are creating the hitboxes on the server, the only fix for this would be if you made them on the client instead.

2 Likes

If the hitboxes are on the server, these tutorials probably tried to mitigate the latency by predicting the characters position on the client by the moment the signal reaches the server. If this is the case, the principle should be able to be applied regardless of how many hitboxes there are.

Though I do recommend just do it all on the client and avoid all the hassle.

2 Likes

How would I transfer this to the client? Sorry it took so long for me to respond, I was on a trip