It doesn't detect the player, that get target with the mouse

Hello I found this script that print the player name that got clicked by the mouse:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()


mouse.Button1Down:Connect(function()

local model = mouse.Target:FindFirstAncestorOfClass('Model')

if model then
	

local clickedPlayer = game.Players:GetPlayerFromCharacter(model)



if clickedPlayer then

print(clickedPlayer.Name)
		

		    end
       end
end)

It doesn’t work, but 3 years ago yes. Any solutions?
The parent of the local script is StarterPlayerScript

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	local Target = Mouse.Target
	if Target then
		local Model = Target:FindFirstAncestorOfClass('Model')
		if Model then
			local ClickedPlayer = game.Players:GetPlayerFromCharacter(Model)
			if ClickedPlayer then
				print(ClickedPlayer.Name)
			end
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.