Keybind npc chat error help please

So I made this thing where if you get close to a npc some gui with the letter E shows up to talk to them then idk what happened but like 10 minutes ago as im writing this it decided to not work anymore, I have not done anything to it and I checked output it said
HumanoidRootPart isn’t a valid member of model
Line 4

Heres the script

while wait() do
script.Parent.Visible = false
for i,v in pairs(game.Workspace.NPCS:GetChildren()) do – Finds all the children from the folder. In this case it will find all the NPCs
local mag = (v.UpperTorso.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude – Makes a variable for the position between the character and the npc.
if mag <= 10 then – If the magnitude is less than or equal to 10 studs then
local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(v.UpperTorso.Position) – The position of the NPC’s Torso to 2d
script.Parent.Visible = true
script.Parent.Position = UDim2.new(0,WSP.X,0,WSP.Y)
script.Parent.TextLabel.Text = "to chat with "…v.Name – Refreshes the text and puts the name of the npc
end
end
end

Tried renaming it didnt work as well

If you want help please make it as easy as possible for us to look through the script. I.e use code formatting that is supported in the devform, once that is done I will come back and take a look. Don’t have time to open studio!

If the error says “HumanoidRootPart isn’t a valid member of model”, it means the loop scans for the HumanoidRootPart before it has actually spawned.

You could try something like:

repeat wait(1) until game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

or

repeat wait(1) until game.Players.LocalPlayer.Character
Char = game.Players.LocalPlayer.Character
repeat wait(.1) until Char:FindFirstChild("HumanoidRootPart")

This is just going off of the error in your output. I didn’t actually read your script because of the formatting.