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)