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

01local RunService = game:GetService("RunService")
02local Pkayers = game:GetService("Players")
03local humanoid = script.Parent
04local root = humanoid.Parent.PrimaryPart
05 
06 
07local targetDistance = 20
08local stopDistance = 5
09 
10function findNearestPlayer()
11local playerList = Players:GetPlayer() -- the error is here
12 
13local nearestPlayer = nil
14local distance = nil
15local 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:

01local NPC = script.Parent
02local PathfindingService = game:GetService('PathfindingService')
03local Players = game:GetService("Players")
04local player
05 
06game.Players.PlayerAdded:Connect(function(client)
07    player = client
08end)
09 
10repeat wait() until player ~= nil
11 
12local path = PathfindingService:CreatePath()
13 
14while wait() do
15    local path = PathfindingService:CreatePath()
View all 37 lines...
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