Position lags behind?

i would like to get the position in front of the player. However, it lags, or ‘desyncs’, when moving.

Here’s what I have: (The ball represents the hitbox)

I’ve tried changing the network ownership of the player’s primarypart, but that just made movement itself laggy.

Here’s my script so far, for reference:

local alt = true

local handle = script.Parent.Handle

local sfx = handle.swing

local animator
local holdAnim

local hits = {handle.A, handle.B}

local pitches = {.8, .9, 1, 1.1, 1.2}

script.Parent.Equipped:Connect(function()
	local humanoid = script.Parent.Parent:FindFirstChildOfClass("Humanoid")
	animator = humanoid:FindFirstChildOfClass("Animator")
	holdAnim = animator:LoadAnimation(script.Hold)
	holdAnim:Play()
end)

script.Parent.Unequipped:Connect(function()
	holdAnim:Stop()
end)

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
	alt = not alt
	local swingAnim = animator:LoadAnimation(script.Swing1)
	if alt then
		swingAnim = animator:LoadAnimation(script.Swing2)
	end
	swingAnim:Play()

	sfx.PlaybackSpeed = pitches[math.random(1, #pitches)]
	sfx:Play()
	
	task.wait(.1)
	--attack--
	
	local PmrPrt = plr.Character.HumanoidRootPart
	local pos = PmrPrt.Position + PmrPrt.CFrame.LookVector * 2.4
	game.Workspace.Part.Position = pos

	local oOverlapParams = OverlapParams.new()
	oOverlapParams.FilterDescendantsInstances = {script.Parent.Parent}
	
	local parts = workspace:GetPartBoundsInRadius(pos, 2, oOverlapParams)
	for i, v in ipairs(parts) do
		if v.Parent:FindFirstChildOfClass("Humanoid") then
			v.Parent:FindFirstChildOfClass("Humanoid").Health -= math.random(15, 20)
			hits[math.random(1, #hits)]:Play()
			return
		end
	end
end)
1 Like

It’s because the character is moving. I think to solve this just add in the humanoid root parts velocity / 2

2 Likes

This works better than trying to figure out server positions lol

Thanks

1 Like

If it’s needed for a hitbox might be worth just using a weld constraint instead so then it is always with the character

1 Like

The following works like a charm:

	local PmrPrt = plr.Character.HumanoidRootPart
	local D = 6
	local Vel = PmrPrt.AssemblyLinearVelocity
	local pos = PmrPrt.Position + PmrPrt.CFrame.LookVector * 2 + Vector3.new(Vel.X/D,0,Vel.Z/D)

It’s just a visual to help with troubleshooting. However, a new problem appears. You can throw “ghost punches” because of client/server ownership issues I guess?? Idk. :confused:

(For context, one must move forward, click, and back up right afterwards.)

This is happening due to latency. The server script is using the velocity of the player at the time of when it receives the event to predict where the hitbox should be; however, it doesn’t account for the possibillity of the character changing its direction of velocity prior to it being updated on the server–which can throw off the prediction as your clip demonstrated.

This is probably the best way to go about making your hitbox while preventing the inaccurate predictions and keeping it simple.

PS - If you want to try to calculate where the hitbox should be while accounting for latency, you’ll find Player:GetNetworkPing() to be pretty useful.

Thanks for the clarification, figured it was something server-related. I’ll try your solutions. thx :D.

2 Likes

Does this look good? I’m new to hitboxes.

	local HBox = game.Lighting.Hitbox:Clone()
	game.Debris:AddItem(HBox,.1)
	HBox.Parent = plr.Character
	HBox.CFrame = hrp.CFrame + hrp.CFrame.LookVector*2
	local W = Instance.new("WeldConstraint",HBox)
	W.Part0 = HBox
	W.Part1 = hrp
	
	local P = workspace:GetPartsInPart(HBox,RcParams)
	
	for i, part in pairs(P) do
		if part.Parent:FindFirstChildOfClass("Humanoid") then
			part.Parent:FindFirstChildOfClass("Humanoid").Health -= math.random(5,15)
			hits[math.random(1, #hits)]:Play()
			return
		end
	end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.