Hi i have been making an aggressive npc and i have run on a couple issues trying to use parallel lua.
Basically it says the functions like computeAsync and CreatePath are not safe, the problem is that sometimes they do work its like a random warning(its not realy an error as it doesn’t send the red text)
Last week it was working fine and now it broke, and before it was still giving those warning but it was still working fine
Keep in mind its my first time making an npc and this has gone through various iterations before even then it can still be improved.
Main function
function task.run(obj)
local Blackboard = obj.Blackboard
local Target = workspace.Alive:FindFirstChild(Blackboard.Target)
local HMR = obj.Instance.HumanoidRootPart
if not Target then return end
local Waypoints = obj.CreateWaypoints(obj.Instance, Target.HumanoidRootPart.Position)
if Waypoints then
Blackboard.MovementData.NextDestination = Waypoints[2]
Blackboard.MovementData.WaypointIndex = 2
Blackboard.MovementData.Waypoints = Waypoints
obj.SharedTable.Destination = Blackboard.MovementData.NextDestination
Blackboard.IsChasing = true
end
if ((Blackboard.MovementData.NextDestination - HMR.Position) * Vector3.new(1,0,1)).Magnitude <= Blackboard.MovementData.DestinationDistanceThreshold then
Blackboard.MovementData.WaypointIndex += 1
if Blackboard.MovementData.Waypoints[Blackboard.MovementData.WaypointIndex] then
Blackboard.MovementData.NextDestination = Blackboard.MovementData.Waypoints[Blackboard.MovementData.WaypointIndex]
obj.SharedTable.Destination = Blackboard.MovementData.NextDestination
else
Blackboard.IsChasing = false
Blackboard.MovementData.NextDestination = Vector3.new(0, 0, 0)
Blackboard.MovementData.Waypoints = {}
Blackboard.MovementData.WaypointIndex = 2
obj.SharedTable.Destination = nil
return FAIL
end
end
obj.SharedTable.AiDecision = "Chase"
return SUCCESS
end
PathFinding function
local function CreateWaypoints (Character, Destination)
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 6,
AgentCanClimb = true,
AgentCanJump = true,
Costs = {
Water = 20
}
})
path:ComputeAsync(Character.PrimaryPart.Position, Destination)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
local MovementWaypoints = {}
for i, waypoint in waypoints do
MovementWaypoints[i] = waypoint.Position
end
return MovementWaypoints
end
end