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
01 | local RunService = game:GetService( "RunService" ) |
02 | local Pkayers = game:GetService( "Players" ) |
03 | local humanoid = script.Parent |
04 | local root = humanoid.Parent.PrimaryPart |
05 |
06 |
07 | local targetDistance = 20 |
08 | local stopDistance = 5 |
09 |
10 | function findNearestPlayer() |
11 | local playerList = Players:GetPlayer() -- the error is here |
12 |
13 | local nearestPlayer = nil |
14 | local distance = nil |
15 | local direction = nil |
Please help
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:
01 | local NPC = script.Parent |
02 | local PathfindingService = game:GetService( 'PathfindingService' ) |
03 | local Players = game:GetService( "Players" ) |
04 | local player |
05 |
06 | game.Players.PlayerAdded:Connect( function (client) |
07 | player = client |
08 | end ) |
09 |
10 | repeat wait() until player ~ = nil |
11 |
12 | local path = PathfindingService:CreatePath() |
13 |
14 | while wait() do |
15 | local path = PathfindingService:CreatePath() |
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