AIs damaging players without touching the player (server lag)

So, i have these NPCs that will attack the player and damage them. But the issue im experiencing is that server lag can heavily affect combat for players. The NPC can damage players with out the NPC actually touching you. (distance depends on the amount of lag the player has) Basically, i wan’t the NPC to actually touch you, to damage you, with out lag being an issue. I’m sure many of you have experienced this issue before. How would i fix this issue?

Extra information
1 Like

If the game is singleplayer just check for collision on the client. Move your collision code to a localscript and tell the server how much damage to deal.

What about :SetNetworkOwner()? could i make use of that? I would like to try and avoid using LocalScripts, due to the way how the NPCs damaging system works

It’s a little hard to diagnose the issue, as there could be errors in different sections such as scripting, physics, server to client replication, logic, etc. Providing your code, linking the place or showing a video of the issue could increase our chances to finding a solution.

Speaking in the general idea, I too as well experienced issues with the Touched event, the problem being caused by server to client replication. However a user suggested to me to use Raycasting to counter this problem of replication. If you would like to see how they helped me you can see forum post below.

1 Like

If you are going to detect collision on the server then you have to accept that there will be a slight discrepancy between collisions detected on the server vs what you actually see on your client. I don’t think :SetNetworkOwner() will do anything.

How would i get it to work with my code? I don’t know much about ray casting

Handle.Touched:Connect(function(hit)
	if hit and hit.Parent and Attacking == true then
		local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		local HumanoidToDamage = hit.Parent:FindFirstChildOfClass("Humanoid")
		if plr and HumanoidToDamage and Humanoid.Health > 0 then
			Attacking = false
			HumanoidToDamage:TakeDamage(Damage.Value)
			if Ability.Value then
				Ability.Value:Clone()
				Ability.Value.Parent = HumanoidToDamage
				Ability.Value.Disabled = false
			end
			HitSound:Play()
			print("Damaged " .. hit.Parent.Name .. " (-" .. Damage.Value .. ")" )
		end
	end
end)