You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
To make the script work -
What is the issue? Include screenshots / videos if possible!
Well the error is saying I cant set it to nil -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Tried searching forums, haven’t found a solution
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Enemy = script.Parent
local Humanoid = Enemy:WaitForChild("Humanoid")
Enemy.PrimaryPart:SetNetworkOwner(nil)
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local WalkSpeed = Enemy:GetAttribute("WalkSpeed")
local SprintSpeed = Enemy:GetAttribute("SprintSpeed")
local Damage = Enemy:GetAttribute("Damage")
local function getPath(destination)
local path = Pathfinding:CreatePath({
AgentHeight = 6;
AgentRadius = 3;
AgentCanJump = false;
AgentCanClimb = false;
Costs = {
Water = 100;
DangerZone = math.huge
}
})
path:ComputeAsync(Enemy.HumanoidRootPart.Position, destination.Position)
return path
end
local function findTarget()
local nearestTarget
local MaxDistance = 44
for index, player in pairs(Players:GetPlayers()) do
if player.Character then
local target = player.Character
local distance = (Enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < MaxDistance then
nearestTarget = target
MaxDistance = distance
end
end
end
return nearestTarget
end
local function Capture(target)
local distance = (Enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 3 then
Humanoid:MoveTo(target.HumanoidRootPart.Position)
else
target.Humanoid:TakeDamage(Damage)
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target then
Capture(target)
Enemy.Humanoid.WalkSpeed = SprintSpeed
break
else
Enemy.Humanoid.WalkSpeed = WalkSpeed
Humanoid:MoveTo(waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
end
else
Humanoid:MoveTo(destination.Position - (Enemy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function Patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomWaypoint = math.random(1, #waypoints)
walkTo(waypoints[randomWaypoint])
end
while wait(0.01) do
Patrol()
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.