You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve?
- I want my shark npc to follow the MoveTo path i have set within a spawn function
- What is the issue?
- The issue is that about 80 to 90 seconds into my shark traveling to and from the five 'shark points" i have set, it suddenly becomes idle and slow, then even starts to swim backwards (see video clip, issue begins around 4 second mark).
- 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)