Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Hello i'm trying to make a npc chasing script but it dont work. how do i make it work?

Asked by 2 years ago
Edited 2 years ago

I'm trying to make a killer script that chases you but i just get this error saying

'attempt to index nil with 'GetPlayer' ' Someone help

heres is the beginning of the code

local RunService = game:GetService("RunService")
local Pkayers = game:GetService("Players")
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart


local targetDistance = 20
local stopDistance = 5

function findNearestPlayer()
local playerList = Players:GetPlayer() -- the error is here

local nearestPlayer = nil
local distance = nil
local direction = nil

Please help

0
Hi, when you were defining the variable "Players", you typed "Pkayers" instead of the word Players. fiddyfiddler 18 — 2y
0
Also you did GetPlayer and it's supposed to be GetPlayers MattVSNNL 620 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

It seems like your new to scripting, here Is a pathfinding script, basically this script with follow the player but also uses pathfinding (Meaning It won't just go In a straight line chasing the player)

script:

local NPC = script.Parent
local PathfindingService = game:GetService('PathfindingService')
local Players = game:GetService("Players")
local player

game.Players.PlayerAdded:Connect(function(client)
    player = client
end)

repeat wait() until player ~= nil

local path = PathfindingService:CreatePath()

while wait() do
    local path = PathfindingService:CreatePath()

    path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)

    if path.Status == Enum.PathStatus.Success then
        local Positions = path:GetWaypoints()

        for i,v in pairs(Positions) do
            local ball = Instance.new("Part", workspace)

            ball.Position = v.Position
            ball.Anchored = true
            ball.CanCollide = false
            ball.Size = Vector3.new(4,4,4)
            ball.Transparency = 0
            ball.Shape = Enum.PartType.Ball

            NPC.Humanoid:MoveTo(ball.Position)
            NPC.Humanoid.MoveToFinished:Wait(2)
            ball:Destroy()
        end
    end
end
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Hi there,

Before you even think about moving your NPC to the nearest player, you need to know more information in regardance to > PathFindingService, You need to know this elsewise it will not be able to detect the nearest player/object.

I recommend checking this article out, https://developer.roblox.com/en-us/api-reference/class/PathfindingService

Answer this question