I have an issue with getting player from Mouse.Target

Hello, I have an issue with this script. It is parented to a tool. When I click on player, it does nothing. How can I fix? Thanks in advance.

local plr = game:GetService("Players").LocalPlayer
local plrGui = plr.PlayerGui
local cuffGui = plrGui.CuffGUI
local players = game:GetService("Players")
local owner = 289079558

	local mouse = plr:GetMouse()
	local target = mouse.Target
	for _, value in pairs(game:GetService("Players"):GetPlayerFromCharacter(target.Parent).Backpack:GetChildren()) do
		local Button = game:GetService("ReplicatedStorage"):WaitForChild("ToolButton"):Clone()
		Button.Name = value.Name
		Button.Text = value.Name
		Button.Parent = cuffGui:WaitForChild("ItemsFrame")


if addedplr.UserId = owner then

Mouse.Target automatically filters any other Player Characters. You need to Raycast the mouse yourself

local Camera = workspace.Camera
local UserInputService = game:GetService("UserInputService")
local function raycast()
	local ViewportSize = Camera.ViewportSize
	local MousePosition = UserInputService:GetMouseLocation()
	local reference = Camera:ViewportPointToRay(MousePosition.X, MousePosition.Y)
	return workspace:RayCast(reference.Origin, reference.Direction*999)

I am pretty sure I don’t need to raycast it myself.

Weird wording on my part. Although what I said is that Mouse.Target just ignores any other Character (You can try printing Mouse.Target while hovering over another Player).

Mouse.Target is showing literally anything with .Locked being false as long as it’s in the 3D space. I have done my own work-around for this issue with this spaghetti-like code;

if mouse.Target and mouse.Target:FindFirstAncestorWhichIsA("Model") and mouse.Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(mouse.Target:FindFirstAncestorWhichIsA("Model")) then
    local player = game:GetService("Players"):GetPlayerFromCharacter(mouse.Target:FindFirstAncestorWhichIsA("Model"));
    -- code here

I don’t recommend you doing that unless you seriously need only players for mouse.Target, and I am 100% sure there are better ways of doing that. This workaround already takes hats/accessories into account.

FindFirstAncestorWhichIsA and FindFirstAncestorOfClass seem to both behave differently from another which makes the latter error while the first returns Workspace.

To be fair I sent a script I’ve made in like 10 seconds.

I’ll just send Nano’s script for that, which works similarly;

if mouse.Target and mouse.Target:FindFirstAncestorWhichIsA("Model") and mouse.Target:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid") then
--- Same premise tl;dr


This still doesn’t work with your own Character and also Roblox mentions that Player:GetMouse() is mostly replaced by UserInputService and ContextActionService.

Mouse has been superseded by UserInputService and ContextActionService, which cover a broader scope, are more feature rich, and support cross-platform patterns better. It remains supported because of its widespread use, but you should strongly consider using these alternatives.

Although I consider this is more advice for @DarkSide_091 rather than you.

Sorry. I will have to learn to use UserInputService and this made me a bit irritated.

I fixed it.

local plr = game:GetService("Players").LocalPlayer
local Mouse = plr:GetMouse()

--  Functions

   If Mouse.Target then
   local TargetPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
   if TargetPlayer then