Determine Player Name

Is there a way to, in script, to determine the name of the player you are currently looking at and place it into a variable? If so what would the code be? And can you provide a sample?


function GetLookatName(ply)
    local trace = ply:GetEyeTraceNoCursor()
    
    if IsValid(trace.Entity) and trace.Entity:IsPlayer() then
        return trace.Entity:Nick()
    end
    
    return ""
end


A function that returns the name of whoever ply is looking at, you can then do


local lolname = GetLookatName(myplayer)

to put it in a variable

(If you’re doing it clientside, just stuff LocalPlayer() instead of “myplayer” to the function)

Got it, thanks!