NPC client server desync

Howdy

https://gyazo.com/f75ebcded6b1f73e745d32c60fb1120a

(left is server, right is client)

i’ve been having issues with NPCs chasing players, my npcs chase just fine on server and are where i want them to be and even damage the player however on client they lag noticeably behind.

things i’ve tried

-setting network ownership to nil, once, every few seconds, every single part on the npc, nothing seems to work
-removing everything but the chase script (same stuff)
-lots of googling

im open to any solutions at this point im kinda lost

local function main(AI, Root, torso, humanoid)
local target = findTarget(AI, Root)

if target then

	if checksight(humanoid,Root,AI,target) then
		humanoid:MoveTo(target.Position - CFrame.new(Root.Position, target.Position).LookVector * -5,target)
		--humanoid:MoveTo(target.Position + target.AssemblyLinearVelocity,target)

		--targetPosition,shooterPosition,targetVelocity,projectileSpeed

		if findDist(target,Root) < 6 then
			if not AI:GetAttribute("attack_cooldown") then
				task.spawn(function()
					AI:SetAttribute("attack_cooldown",true)

					attack(target,Root,humanoid,AI)
				end)
			end
		end
	else
		pathToTarget(AI,Root,humanoid,target)
	end
else
	if not AI:GetAttribute("wander_cooldown") then
		AI:SetAttribute("wander_cooldown", true)

		walkRandomly(AI,Root,humanoid)

		task.delay(2, function()
			AI:SetAttribute("wander_cooldown", false)
		end)
	end
end

end

(code)

1 Like

Well that’s just kinda Filtering Enabled for ya. The only thing I can think of right now is trying to have a separate NPC on both the server and the client.

That’s probably not what you desired but I don’t really have anything else on my mind rn

Thats just how replication works in roblox, you dont really should worry about that.
But if you are curious enough, set the character network owneship to server, and then you will notice a huge input delay, but no difference between server and client in character position

1 Like

i have done that
assuming setting networkownership to nil is that

1 Like

not exactly sure how to do something like that, and SURELY there’s a better way
(clueless)

1 Like

hey everyone after a couple of weeks sitting on it i did some more reading and came up with a solution! getting the players position and npcs position from client and passing that info to the server and moving it on server helps A LOT and can be further improved by telling the NPC to go like 0.5-1 studs ahead of the target instead of the 5~ i had to do previously

https://gyazo.com/7c733ff361448231023398e24de2867d (old)

https://gyazo.com/afd184b324f3c788fdbc835c4c6c5be1 (new)

while not perfect it’s close enough for me to stop worrying about it

(here’s the code)

SERVER

local player = playerservice:GetPlayerFromCharacter(target.Parent)
				Target_Ping_Request:FireClient(player,Root)

				local clientplayerpos = nil

				Target_Ping_Request.OnServerEvent:Connect(function(player,clientpos,chaserpos)

					clientplayerpos = clientpos
					humanoid:MoveTo(clientplayerpos - CFrame.new(chaserpos, clientplayerpos).LookVector * 0,target)
				end)

CLIENT

ping_request.OnClientEvent:Connect(function(chaser)
	ping_request:FireServer(character.Torso.Position,chaser.Position)
	
end)

double update, i found that if you set the networkownership to the target player it’s like 80x more smooth, no needing to lead or anything, i dont understand why i couldnt find anything referencing that, anyway hope it helps

local player = playerservice:GetPlayerFromCharacter(target.Parent)

				if player ~= nil then
					for i, v in pairs(AI:GetDescendants()) do
						if v:IsA("BasePart") and v.Parent.Parent == workspace then
							v:SetNetworkOwner(player)
						end
					end
				end

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