Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

2 comments

0

Hi, when you were defining the variable "Players", you typed "Pkayers" instead of the word Players.

0

Also you did GetPlayer and it's supposed to be GetPlayers

2 answers

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

This discussion is preserved as it was. New answers, comments, and votes are closed.