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() -- No this is not the problem (I already commented it out and nothing fixed)
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) -- Yes I made sure this is NEVER called
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 -- Halt setup never prints so it isn't this affecting my NPCs
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() -- This is where I put my AI inside the NPC.
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)