Problem with Hitboxes spawning behind the player while moving

Hey! Im (kinda) a beginner scripter trying to make a fighting game. I am currently working on my Hitbox system and ive noticed it lags behind when the player moves (Which i dont want). Ive done some research but most of the solutions there uses welding parts to the player. My hitbox system dosent use parts and uses GetPartBoundsInBox instead so im not really sure what to do. What should I do to prevent my hitboxes from “lagging” behind? I really dont wanna rewrite my hitbox system.

local Remote = game.ReplicatedStorage:FindFirstChild("TestRemote")

Remote.OnServerEvent:Connect(function(player, attackType)
	local char = player.Character
	local HRP = char.HumanoidRootPart

	if attackType == "M1 Normal" then
		local hitboxPos = HRP.CFrame * CFrame.new(0,0,-3)

		local filterObjects = {char}
		local hitboxSize = Vector3.new(5,5,5)
		local maxObjectsAllowed = 5
		local params = OverlapParams.new(filterObjects,Enum.RaycastFilterType.Exclude,maxObjectsAllowed,"Default")
		local hitObject = workspace:GetPartBoundsInBox(hitboxPos,hitboxSize,params)
		
		local HitboxVisual = Instance.new("Part")
		HitboxVisual.Anchored = true
		HitboxVisual.CanCollide = false
		HitboxVisual.CanQuery = false
		HitboxVisual.CanTouch = false
		HitboxVisual.CFrame = hitboxPos
		HitboxVisual.Size = hitboxSize
		HitboxVisual.Transparency = 0.5
		HitboxVisual.Parent = game.Workspace
		game.Debris:AddItem(HitboxVisual,0.25)
		
		local hitPlayers = {}		
		for i,hit in pairs(hitObject) do
			if hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Parent.Name ~= player.Name and not table.find(hitPlayers, hit.Parent) then
				table.insert(hitPlayers, hit.Parent)
				hit.Parent.Humanoid.Health -= 10
				print(hit.Parent.Humanoid.Health)
			end
		end
		print(hitPlayers)
	end
end)
2 Likes

Why not Weld that CanCollide Massless Transparent Part that’s 5x5x5 to the player? You’re already creating it anyway. Welding it to the Player makes it move with the Player.

It lags behind because of client-server latency. Basically, it takes a moment for the server or client to update each other on where something is, like a moving character.

The best solution is to use client-sided hitboxes, which is structured like this:

  1. Hitbox Start
  2. Send remotevent to client telling it to create hitbox.
  3. Run the hitbox on the client, firing a remote event upon hitting a target.
  4. On server, listen for the remote event fired when the client hits something.
  5. Sanity check what the client sends to make sure it’s not hitting things it shouldn’t.

I see… ill give it a try! Many thanks!

that part is purely visual gug

Yes, but it can do both visual and functional and give you no lag. If you want it invisible later just make it transparent.