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
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)
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
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’
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