Hitbox Help (CLIENT)

Hello, I’ve been working on a Basic Hitbox System, and I just need feedback on my code and how I can insert sanity checks.

Things that the hitbox works with:

  • Works with a big map,
  • Tons of parts

Things I want to get done:

  • Sanity checks,
  • Optimization

Here’s the Client code:

if not game.Loaded then game.Loaded:Wait() end
print("Hitbox Loaded")

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local HitboxPart = workspace:WaitForChild("Part")

RunService.Heartbeat:Connect(function()
	if not Character or not HumanoidRootPart or not HitboxPart then return end
	local PingOffsetSeconds = Player:GetNetworkPing()
	local ClampedPing = math.clamp(PingOffsetSeconds, 0, 0.3)
	
	local PlayerVelocity = HumanoidRootPart.AssemblyLinearVelocity
	local PredictedOffsetVector = PlayerVelocity * ClampedPing
	
	local LocalPredictedOffsetVector = HumanoidRootPart.CFrame:VectorToObjectSpace(PredictedOffsetVector)
	local NewCFrame = HumanoidRootPart.CFrame * CFrame.new(LocalPredictedOffsetVector)
	
	HitboxPart.CFrame = NewCFrame
end)
1 Like

Can anyone say if this is a good idea, what if when I request the Hitbox location of the hit player, I check the magnitude of the hitbox? Making sure that the Hitbox is at least within 5 studs or lower of the Player?

your usage of heartbeat is correct and your usage of network ping is very inclusive.

and your hitbox location requesting should work well.
you can do this:

local distance = (HitboxPart.Position - HumanoidRootPart.Position).Magnitude
if distance <= 5 then
    --this will check if the distance is within 5 studs or equal to it
end

if you are looking to further advance this, use raycasts and take into account some distancing factors.

Thank you, Oliver, and I’ll look into this.

One more question how I can implement this into a tool, like for example:

  1. Detect when Player clicked.
  2. Check if Player can click.
  3. Return all Hitbox’s on SERVER.
  4. Check if the Hitbox is within 5 studs of the Player.
  5. Magnitude checks and creates a Hitbox on the Melee and detect if the Melee Hitbox is touching the hitbox.

Do you have any more ideas how I can do this