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.