Needing help with a error that happens 50/50 of the time

this is a name script, when u hover over a player, has a text label with there name

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local screenGui = script.Parent
local frame = screenGui.TextLabel

local function MoveMouse(...)
	local xDimension = mouse.X
		local yDimension = mouse.Y
		local xResolution = screenGui.AbsoluteSize.X
		local yResolution = screenGui.AbsoluteSize.Y
		local framePosXStart = math.round(xResolution * frame.Position.X.Scale) + frame.Position.X.Offset
		local framePosXEnd = framePosXStart + math.round(xResolution * frame.Size.X.Scale) + frame.Size.X.Offset
		local framePosXMid = math.round((framePosXEnd - framePosXStart)/2)
		local framePosYStart = math.round(yResolution * frame.Position.Y.Scale) + frame.Position.Y.Offset
		local framePosYEnd = framePosYStart + math.round(xResolution * frame.Size.Y.Scale) + frame.Size.Y.Offset
		local framePosYMid = math.round((framePosYEnd - framePosYStart)/2)
	frame.Position = UDim2.new(0, xDimension-framePosXMid, 0, yDimension-framePosYMid)
	
end



wait(3)




while true do
	wait()
	if mouse.Hit.Parent:FindFirstChild("Humanoid") then --check to make sure Mouse.Target is child of a character
		local Character = mouse.Target.Parent
		screenGui.Enabled = true
		frame.Text = mouse.Target.Parent.Name
		mouse.Move:Connect(MoveMouse)
	elseif not mouse.Target.Parent:FindFirstChild("Humanoid") then
		screenGui.Enabled = false
		mouse.Move:Connect(function()
			return
		end)
	end
end



This is working 50% of the time, while the other time it is returning Players.lumbergrim.PlayerGui.ScreenGui.LocalScript:29: attempt to index nil with ‘Parent’

script.parent.parent.parent leads to LocalPlayer in your script.

this should work

while true do
	task.wait()
	if mouse.Target and mouse.Target.Parent and mouse.Target.Parent:FindFirstChild("Humanoid") then --check to make sure Mouse.Target is child of a character
		local Character = mouse.Target.Parent
		screenGui.Enabled = true
		frame.Text = mouse.Target.Parent.Name
		mouse.Move:Connect(MoveMouse)
	else
		screenGui.Enabled = false
		mouse.Move:Connect(function()
			return
		end)
	end
end

Worked, Thanks, also what was causing the issue?

sorry for responding so late, what you were doing is not checking if the mouse was actually hitting something (if mouse.Target was actually existent), so sometimes it returned nil.