NPC Network Ownership Failing for no apparent reason?

I’m facing a difficult problem with my NPC system.

My NPC system works by spawning the NPC on server and then setting the Network Ownership of the NPC to the Player that I want to control the NPC.

The NPCs are moved around by LinearVelocity manipulation on Client and AlignOrientation to make the NPC face towards my selected direction. (I’m not sure if this affects the NPC at network ownership)

This all works fine until I change the Scale of my NPC from 1 to 2 (The model). The NPC flashes once and then NetworkOwnership is lost.

Here is the main NPC handler:

-- THIS IS THE FUNCTION THAT SPAWNS THE NPC

function NPCHandler.LoadAI(NPC:Model, AIName:string, NetworkOwner:Player)
	if RunService:IsClient() then
		local Handler = ItemsModulesTbl[AIName]
		if Handler~=nil then
			Handler.LoadAI(NPC, NetworkOwner)
		end
	end
	if RunService:IsServer() then
		if typeof(NetworkOwner)=="Instance" then
			if NetworkOwner:IsA("Player") then
				local Handler = ItemsModulesTbl[AIName]
				if Handler~=nil then
					local Halted = false
					local Loaded = false
					Handler.Setup(NPC, NetworkOwner, function()
						if Halted==true then return end
						-- Done loading
						Loaded = true
						NPC:SetAttribute("AI", AIName)
						ClientRemote:FireAllClients({ST = "LoadAI", N = NPC, A = AIName, O = NetworkOwner})
					end)
					task.delay(3, function()
						if Loaded==false then
							Halted = true
							Handler.HaltSetup(NPC)
							NPCHandler.LoadAI(NPC, AIName, NetworkOwner)
						end
					end)
				end
			end
		end
	end
end

Now this next module is included for EVERY NPC

-- THIS IS THE MODULE THAT EACH NPC HAS WHICH IS SLIGHTLY DIFFERENT PER NPC SETUP.
function CurrentHandler.HaltSetup(NPC:Model)
	if NPC~=nil then
		 if NPC.Parent~=nil then
			if LoadingThreads[NPC]~=nil then
				task.cancel(LoadingThreads[NPC])
				LoadingThreads[NPC] = nil
				NPC:SetAttribute("NetworkOwner", "")
				local HRP = NPC:FindFirstChild("HumanoidRootPart") :: BasePart
				if HRP~=nil then
					HRP:SetNetworkOwner(nil)
				end
			end
		 end
	end
end

function CurrentHandler.Setup(NPC:Model, NetworkOwner:Player, Loaded)
	if NPC~=nil then
		if NPC.Parent~=nil then
			if RunService:IsServer() then
				LoadingThreads[NPC] = task.spawn(function()
					local HRP = NPC:WaitForChild("HumanoidRootPart") :: BasePart
					local Humanoid = NPC:WaitForChild("Humanoid") :: Humanoid

					if NetworkOwner~=nil then
						NPC:SetAttribute("NetworkOwner", NetworkOwner.Name)
						HRP:SetNetworkOwner(NetworkOwner)
						Loaded()
					end
				end)
			end
		end
	end
end


function CurrentHandler.LoadAI(NPC:Model, NetworkOwner:Player)
	if RunService:IsServer() then warn("AI is made to be used on Clients!") return end
	
	local FoundActor = NPC:FindFirstChildOfClass("Actor")
	if FoundActor~=nil then
		warn("Prevented AI Duplication [CLIENT]")
		FoundActor:Destroy()
	end
	
	local AI_LocalScript = script:WaitForChild("Actor"):Clone()
	AI_LocalScript.Parent = NPC
	AI_LocalScript:FindFirstChildOfClass('Script').Enabled = true
end

Lastly, my AI script (the script which handles the pathfinding), does not have any issues with it so I won’t list it, It’s also client so I refuse to believe that its the one causing the problem

REMEMBER! – The NPC works COMPLETELY fine when the scale is 1.

VIDEOS SO YOU CAN WATCH THE PROBLEM:

https://youtu.be/wcGPmx-02eg – Working NPC [Scale 1]

https://youtu.be/VDkTFblQtE8 – Broken NPC [Scale 2]

(When the color turns white it means the NetworkOwnership was lost)

wrong category, move it to #scripting-help

alright ill move it now, thanks.

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