So I have this NPC, and it works, unless you walk behind it or around it. I get the error 'Workspace.NewNpc.Dummy.AI:94: attempt to index global 'prs' (a nil value)'.
The script's really long, sorry if this hurts your eyes.
local ts = game:GetService("TweenService")
local event = Instance.new("BindableEvent")
event.Name = ("StartAttacking")
event.Parent = script.Parent.Parent
wait(5)
script.Parent.AttackingPlayer.Value = ""
local range = 20
local function searchforplayer()
local ray = Ray.new(script.Parent.HumanoidRootPart.CFrame.Position, script.Parent.HumanoidRootPart.CFrame.LookVector * 500)
local hrp = script.Parent.HumanoidRootPart.Position
local num = 8
local part = game.Workspace:FindPartOnRay(ray)
local person = nil
if part then
if part.Parent:FindFirstChild("Humanoid") then
if game.Players:FindFirstChild(part.Parent.Name) then
print("Found player!")
print(part.Parent.Name)
person = part.Parent
end
end
end
if person == nil then
local region = Region3.new(Vector3.new(hrp.X - num, hrp.Y - num, hrp.Z - num), Vector3.new(hrp.X + num, hrp.Y + num, hrp.Z + num))
local parts = game.Workspace:FindPartsInRegion3(region)
for a, b in pairs(parts) do
if b.Name == ("HumanoidRootPart") then
print(b.Name)
person = b.Parent
end
end
else
return person()
end
if person ~= nil then
return person
end
end
while true do
if script.Parent.AttackingPlayer.Value == "" then
print("Searching...")
if searchforplayer() == nil then
while searchforplayer() == nil do
wait(0.1)
if searchforplayer() ~= nil then
script.Parent.AttackingPlayer.Value = searchforplayer().Name
script.Parent.Head["!Gui"].Frame.TextLabel.Visible = true
script.Parent.Humanoid.JumpPower = 48
script.Parent.Humanoid.Jump = true
wait(1)
script.Parent.Head["!Gui"].Frame.TextLabel.Visible = false
script.Parent.Humanoid.JumpPower = 0
break
end
end
end
end
local path = game:GetService("PathfindingService"):CreatePath()
if script.Parent.AttackingPlayer.Value ~= ("") then
local prs = game.Workspace:FindFirstChild(script.Parent.AttackingPlayer.Value)
end
if script.Parent.AttackingPlayer.Value ~= ("") then
event:Fire(script.Parent.AttackingPlayer.Value)
end
range = math.huge
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, prs.HumanoidRootPart.Position)
local points = path:GetWaypoints()
local player = game.Workspace:FindFirstChild(script.Parent.AttackingPlayer.Value)
script.Parent.Parent.Points:ClearAllChildren()
for a, b in pairs(points) do
part = Instance.new("Part")
part.CanCollide = false
part.Size = Vector3.new(1,1,1)
part.Position = b.Position
part.Anchored = true
part.Transparency = 1
part.Parent = script.Parent.Parent.Points
end
if path.Status == Enum.PathStatus.Success then
for a, b in pairs(points) do
script.Parent.Humanoid:MoveTo(part.Position)
if b.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
end
end
end
end
I get this error if I stand in front of the guy, or be around him. Any help?
