Attempt to indext nil with 'Position'

I’m trying to make an NPC chat system but this error keeps happening: “Attempt to index nil with ‘Position’”

My code:

	if detected == false and chatting == false then
		for i, NPC in pairs(npcs:GetChildren()) do
			local Humanoid = NPC:FindFirstChild("Humanoid")
			local HMR = NPC:FindFirstChild("HumanoidRootPart")

			if Humanoid and HMR then
				if (HMR.Position - plrHMR.Position).magnitude < 15 then  --where the error is happening
					detected = true
					detectedNpc = NPC
					eButton.Visible = true

I have no idea why it’s happening and it’s driving me crazy.

Where are you defining “plrHMR”?

It is at the top of the script, not in any functions.

Could you send the line(s) of code where you define it?


local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

-- Character

local char = player.Character
local plrHMR = char:FindFirstChild("HumanoidRootPart")
local char = player.Character or player.CharacterAdded:Wait()

Or potentially:

local plrHMR = char:WaitForChild("HumanoidRootPart")