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