Players position error

Hello! So I am working on a npc that will follow a path and if it sees the player it chases it down. Basically am working on a scary game. I recenly got help with someone on devfourm on the script but am having a issue. The npc doesnt follow the player and gives the error “Workspace.Boss.NPCScript:23: attempt to index nil with ‘Position’ - Server - NPCScript:23”

The script is inside the spider and is a script

local MoveParts = NPC.MoveParts
local humanoid = NPC:WaitForChild("Humanoid")
local pathes = {
	MoveParts.Part.Position,
	MoveParts.Part2.Position,
	MoveParts.Part3.Position,
	MoveParts.Part4.Position
}
local pathtarget = 0

NPC.Humanoid.MoveToFinished:Connect(function()
	wait(3)
	if pathtarget ~= -1 then -- check if following a player
		pathtarget = ((pathtarget + 1) % #pathes) -- cycle path targets from 0 to length of pathes - 1
		NPC.Humanoid:MoveTo(pathes[pathtarget+1]) -- go to next path target 
	end
end)

function FollowPlayer(playerhrp) -- player humanoid root part
	pathtarget = -1 -- stop following path
	print("Following player")
	NPC.Humanoid:MoveTo(playerhrp.Position, playerhrp) -- follow player (note this will follow player even if they move)
end
function UnfollowPlayer()
	pathtarget = 0 -- reset path
	NPC.Humanoid:MoveTo(NPC.Humanoid.RootPart.Position) -- cancel follow (also calls MoveToFinished to restart pathes)
	print("Isnt following player")
end

if pathtarget ~= -1 then -- check if following a player
	pathtarget = ((pathtarget + 1) % #pathes) -- cycle path targets from 0 to length of pathes - 1
	NPC.Humanoid:MoveTo(pathes[pathtarget+1]) -- go to next path target 
	--UnfollowPlayer()
	FollowPlayer()
elseif pathtarget ~= 0 then
	FollowPlayer()
end

you have playerhrp as an argument to FollowPlayer, in the function you try to index position from it but you forgot to pass an object through to the function. so it’s trying to get location out of nil

am still learning scripting so this is kind of confusing.

that’s ok, the function FollowPlayer you use on line 34 and 36 seems to be set up for you to put a part from the player in it
something like

player = workspace:FindFirstChild("username")
FollowPlayer(player.Head)

so something like this? i got a error with “Workspace.Boss.NPCScript:33: attempt to index nil with ‘Head’ - Server - NPCScript:33”

	player = workspace:FindFirstChild("username")
	FollowPlayer(player.Head)
elseif pathtarget ~= 0 then
	UnfollowPlayer()
end

you gotta replace the username text with your own username too, not sure how you want to find a player but you can use the players service to find players (Players)

I added game.Players though am geting the same error with the head

it does have to be a player model that’s in workspace. can you show what you added? and how you want to choose the player? you could use Players:GetPlayers() and select a random one if that’s what you need

I kinda want the npc to act like piggy. Like if the bot seees a player than it chases it. if it doesnt then it continunes the path.

local MoveParts = NPC.MoveParts
local humanoid = NPC:WaitForChild("Humanoid")
local pathes = {
	MoveParts.Part.Position,
	MoveParts.Part2.Position,
	MoveParts.Part3.Position,
	MoveParts.Part4.Position
}
local pathtarget = 0

NPC.Humanoid.MoveToFinished:Connect(function()
	wait(3)
	if pathtarget ~= -1 then -- check if following a player
		pathtarget = ((pathtarget + 1) % #pathes) -- cycle path targets from 0 to length of pathes - 1
		NPC.Humanoid:MoveTo(pathes[pathtarget+1]) -- go to next path target 
	end
end)

function FollowPlayer(playerhrp) -- player humanoid root part
	pathtarget = -1 -- stop following path
	print("Following player")
	NPC.Humanoid:MoveTo(playerhrp.Position, playerhrp) -- follow player (note this will follow player even if they move)
end
function UnfollowPlayer()
	pathtarget = 0 -- reset path
	NPC.Humanoid:MoveTo(NPC.Humanoid.RootPart.Position) -- cancel follow (also calls MoveToFinished to restart pathes)
	print("Isnt following player")
end

if pathtarget ~= -1 then -- check if following a player
	player = workspace:FindFirstChild(game.Players)
	FollowPlayer(player.Head)
elseif pathtarget ~= 0 then
	UnfollowPlayer()
end

that’s a little bit harder to do, you probably will need raycasting (Intro to Raycasting) and iterating through the players in the game
maybe something like

players = game:GetService("Players") -- at the top when you're defining variables


for num,plr in ipairs(players:GetPlayers()) -- after all the definitions 
   character = workspace:FindFirstChild(plr.Name)
    -- add code to raycast to a part in character here
end

you’re probably going to want to add a loop for this and the if statement at the bottom so it runs more than once

Thank you! one last thing. How would I fix this error?
Head is not a valid member of Players “Players” - Server - NPCScript:33

	player = game:GetService("Players") 
	FollowPlayer(player.Head)
elseif pathtarget ~= 0 then
	UnfollowPlayer()
end

this is just the players service, so that code tries to look for someone named “Head” in the players category

how would i get it to find the player?

you can use the for loop i sent earlier or you could use this

local Players = game:GetService("Players")
local random = Random.new()
local MoveParts = NPC.MoveParts
local humanoid = NPC:WaitForChild("Humanoid")
local pathes = {
	MoveParts.Part.Position,
	MoveParts.Part2.Position,
	MoveParts.Part3.Position,
	MoveParts.Part4.Position
}
local pathtarget = 0

NPC.Humanoid.MoveToFinished:Connect(function()
	wait(3)
	if pathtarget ~= -1 then -- check if following a player
		pathtarget = ((pathtarget + 1) % #pathes) -- cycle path targets from 0 to length of pathes - 1
		NPC.Humanoid:MoveTo(pathes[pathtarget+1]) -- go to next path target 
	end
end)

function FollowPlayer(playerhrp) -- player humanoid root part
	pathtarget = -1 -- stop following path
	print("Following player")
	NPC.Humanoid:MoveTo(playerhrp.Position, playerhrp) -- follow player (note this will follow player even if they move)
end
function UnfollowPlayer()
	pathtarget = 0 -- reset path
	NPC.Humanoid:MoveTo(NPC.Humanoid.RootPart.Position) -- cancel follow (also calls MoveToFinished to restart pathes)
	print("Isnt following player")
end

if pathtarget ~= -1 then -- check if following a player
        plrlist = players:GetPlayers()
	player = workspace:FindFirstChild(plrlist[random:NextInteger(1,#plrlist)].Name)
	FollowPlayer(player.Head)
elseif pathtarget ~= 0 then
	UnfollowPlayer()
end

this should find a random player and follow them, though without checking if they’re visible to the bot. also untested

hmm I know you didnt test it but for some reason. For line 35. the `.name’ causes a error
Workspace.Boss.NPCScript:35: attempt to index nil with ‘Name’

am guessing because the script couldnt find the player?

I fixed it by added a wait. But same issue with the head

I think you have to make a loop that checks if there are any players near by then if there is put the player’s HumanoidRootPart as the argument for the followplayer function instead of the player.head

the thing is whenever I put any argument. I get the same error.
Workspace.Boss.NPCScript:37: attempt to index nil with ‘HumanoidRootPart’ - Server - NPCScript:37

what does line 37 say on the npcScript?