How do I Improve My Hitbox

I want someone’s opinion on my hitbox:

  • When I run the function to create a hitbox I check if a player or an NPC is using the attack (if it’s a player then I run it on the client, if it’s an NPC I run it on the server)
  • I heard running the hitboxes on the client makes it “smoother”

This is the code from the server:

local function hitbox(user, size, pos)
	if game.Players:GetPlayerFromCharacter(user) then
		local targets = hitboxFunc:InvokeClient(game.Players:GetPlayerFromCharacter(user), user, size, pos)
		return targets
	else
		local cframe = user.HumanoidRootPart.CFrame * CFrame.new(pos)
		
		local targets = game.Workspace:GetPartBoundsInBox(cframe, size)
		local enemies = {}

		for i, target in pairs(targets) do
			if target.Name == "Hitbox" then
				if target.Parent:FindFirstChild("Humanoid") then
					if target.Parent.Name ~= user.Name then
						table.insert(enemies, target.Parent)
					end
				end
			end
		end

		return enemies
	end
end

This is the code from the client:

local RS = game:GetService("ReplicatedStorage")
local hitboxFunc = RS.Events:WaitForChild("HitboxFunc")

hitboxFunc.OnClientInvoke:Connect(function(user, size, pos)
	local cframe = user.HumanoidRootPart.CFrame * CFrame.new(pos)
	
	local targets = game.Workspace:GetPartBoundsInBox(cframe, size)
	local enemies = {}
	
	for i, target in pairs(targets) do
		if target.Name == "Hitbox" then
			if target.Parent:FindFirstChild("Humanoid") then
				if target.Parent.Name ~= user.Name then
					table.insert(enemies, target.Parent)
				end
			end
		end
	end
	
	return enemies
end)

Any suggestions? I’m still confused by the topic of running hitboxes on the client.

1 Like

This is not true. After the hitbox request from a tool I guess, is received by server, it gets sent to client (which is a vulnerability by itself) and then back to server. This is not only unsecure but also less performant.

A “smoother way” (compared to just using part queries) would be some lag compensation method, I haven’t used this module but this could be considered as well:

Usage of InvokeClient is usually avoidable and should be avoided whenever possible because of the said reasons.

1 Like

So should I make it run on the server?

Can you please explain why its less performant? Taking the burden off the server is good especially if there’s multiple hitbox parts

1 Like