NPC goes somewhat idle then backwards during spawn function

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
  • I want my shark npc to follow the MoveTo path i have set within a spawn function
  1. What is the issue?
  1. What solutions have you tried so far?
  • I have searched endlessly on the Hub and online, and have tried the following:
    a. tried to set network ownership for the humanoid as my script shows
    b. tried to work around the MoveToFinished 8 second timeout by following roblox coding, as script also shows.
    c. I have other functions for shark to attack players, but removed it to isolate the issue of the shark not staying indefinitely on the course of finding 1 of 5 random shark points to swim to and from.

Here is my simplified script that focuses on the shark npc issue described above… I have 5 shark points in a folder that has been declared:

local Shark = script.Parent:WaitForChild(“Shark”)

local This_H = script.Parent:WaitForChild(“Humanoid”)

local checkForSharkPoints = game.Workspace:FindFirstChild(“SharkPoints - Enclosed”)

local SharkPoints = checkForSharkPoints:GetChildren()
local LastPoint, ThisPoint

function setNetworkOwner(Shark)
for _, x in pairs(Shark:GetDescendants())do
if x:IsA(“BasePart”) and x:CanSetNetworkOwnership() then
x:SetNetworkOwner(nil)
end
end
end

task.spawn(function()
while task.wait() do
ThisPoint = SharkPoints[math.random(1,#SharkPoints)]
if LastPoint == ThisPoint then
LastPoint = SharkPoints[math.random(1,#SharkPoints)]
end
This_H:MoveTo(ThisPoint.Position)
This_H.MoveToFinished:Wait()
LastPoint = ThisPoint
end

local function moveTo(Shark, ThisPoint)
local targetReached = false

-- listen for the humanoid reaching its target
local connection
connection = Shark.MoveToFinished:Connect(function(reached)
	targetReached = true
	connection:Disconnect()
	connection = nil
end)

-- start walking
Shark:MoveTo(ThisPoint)

-- execute on a new thread so as to not yield function
spawn(function()
	while not targetReached do
		-- does the humanoid still exist?
		if not (Shark and Shark.Parent) then
			break
		end
		-- has the target changed?
		if Shark.WalkToPoint ~= ThisPoint then
			break
		end
		-- refresh the timeout
		Shark:MoveTo(ThisPoint)
		wait(6)
	end

	-- disconnect the connection if it is still connected
	if connection then
		connection:Disconnect()
		connection = nil
	end
end)
end

end)

What is the This_H humanoid doing in here?

This_H is the humanoid of the shark, and it is moving from SharkPoint to another (total of 5 points), rotating through them randomely (from LastPoint to ThisPoint).

Why is there 2 humanoids? One is inside the the Shark variable.

You are correct, sorry it should read:
local Shark = game.Workspace:WaitForChild(“Shark”)

I was declaring the model, so that I can set it for network ownership. I don’t fully understand why, but I was putting it in there as a possible solution.

When i Run the game in studio, the shark doesn’t exhibit the issue… but when I Play in the game in studio, it shows the issue after about 80 seconds.

Ok this solution has worked for my issue:

Seems the network ownership needs to be set to server… and if you are welding anything to the npc then u need to set it again after the weld takes place.

Hope this helps someone else!