Player is returning nil when I hover over?

When I hover over my player it returns nil. However if I hover over a Dummy it works fine. Can anyone help?

Code:

local Player = game:GetService("Players").LocalPlayer;
local Mouse = Player:GetMouse();
local UIS = game:GetService("UserInputService");
local RS = game:GetService("RunService");

local Gui = Instance.new("ScreenGui", Player:WaitForChild("PlayerGui"));
local Label = Instance.new("TextLabel", Gui);
Label.BorderSizePixel = 0
Label.TextScaled = true
Label.BackgroundTransparency = 1
Label.Font = Enum.Font.SourceSansBold
Label.Size = UDim2.new(0, 100, 0, 10);
Label.AnchorPoint = Vector2.new(0.5, 0.5);

RS:BindToRenderStep("MouseHover", Enum.RenderPriority.Camera.Value - 1, function()

    local mPos = UIS:GetMouseLocation();
    Label.Position = UDim2.new(0, mPos.X, 0, mPos.Y);

    local Target = Mouse.Target;

    if Target then
        if Target.Parent then
            if Target.Parent:IsA("Model") and not (Target.Parent == workspace) then
                local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Target.Parent);
                if tPlayer then
                    Label.Text = tPlayer.Name;
                else
                    Label.Text = "Model: " .. Target.Parent.Name;
                end;
            else
                Label.Text = "Object: " .. Target.Name;
            end;
        else
            Label.Text = "Object: " .. Target.Name;
        end;
    else
        Label.Text = "nil";
    end;

end);

Mouse.Target does not actually detect players, hence why it is returning nil

Try extending the mouse’s UnitRay.

local target = workspace:FindPartOnRay(Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 50000))

Mouse.Target is a reference to the BasePart that the mouse is over. If you only need this to work for players, then my strategy normally is to check who’s character the part descends. It’s helpful for arbitrary levels of parts so I don’t have to keep checking the relative path.

local function getPlayerFromPart(part)
    for _, player in ipairs(PlayersService:GetPlayers()) do
        if player.Character and part:IsDescendantOf(player.Character) then
            return player
        end
    end
    return nil
end

-- Usage
local player = getPlayerFromPart(mouse.Target)
if player then
    displayName(player.Name)
end

Though you could always just use FindFirstAncestorOfClass to check for model ancestors, find humanoids in them and then run a check at GetPlayerFromCharacter until it returns nil (no more ancestors of the model class).

I am getting this error, what should I do to prevent it.

19:30:38.040 - RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: ReplicatedFirst.Client:56: attempt to index nil with ‘IsDescendantOf’

The error is self explanatory, please do try debugging it first and using the error as a guiding point. I have also provided a use example below. You’re passing an unaccepted value to that function or are not implementing it correctly as the script is trying to use IsDescendantOf on something that doesn’t exist.

Yes, of course I know why I am getting the error. It is because Mouse.Target is nil. Which is then confusing me.

You can’t assume that dynamically set properties are non-nil. Simply don’t call the function if Mouse.Target is nil.