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

Pathfinding for hostile npc wont work?

Hello, I'm making a action game and i'm making the AI for the enemies. However whenever i run the script I get this error:

Workspace.Skeleton.Control:3: attempt to index field 'LocalPlayer' (a nil value)

This is the script:

while wait() do
	local player = game.Players.LocalPlayer.Character
	local path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.HumanoidRootPart.Position,player.HumanoidRootPart.Position, 200)
	
	if path.Status == Enum.PathStatus.Success then
		local pts = path:GetPointCoordinates()
		if #pts < 3 then
			script.Parent.Humanoid:MoveTo(player.HumanoidRootPart.Position)
		else
			script.Parent.Humanoid:MoveTo(pts[3])
		end
	else
		script.Parent.Humanoid:MoveTo(player.HumanoidRootPart.Position)
	end
end

Help would be much appreciated :)

1 answer

✓ Accepted answer

You are trying to find the local player in a normal script. LocalPlayer can only be accessed in a local script. Hope this helps.

1 comment

0

Thanks so much!!!

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