Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Finding players names using Mouse.Target with a tool equipped?

local Players = game:GetService("Players")
local player = Players.LocalPlayer

	Tool = script.Parent

local Mouse = player:GetMouse()

	Tool.Activated:Connect(function()
		if Tool.Equipped then 
		
			
		if Mouse.Target and Mouse.Target:FindFirstChild("Humanoid") then 
			
			local pos1 = Mouse.Target.Position
	       		local pos2 = player.Character.UpperTorso.Position
		        local magnitude = (pos1 - pos2).magnitude
		
				if magnitude <=13 then
		
		print(Mouse.Target)				
			
            		end
		end
	end
end)

I've used almost identicle code when trying to find a click detector with Mouse.Target but I seem to have no luck when trying to do it with a player to get there name.

1 answer

You can use GetPlayerFromCharacter alng with mouse.Target

local Players = game:GetService("Players")
local player = Players.LocalPlayer

    Tool = script.Parent

local Mouse = player:GetMouse()

    Tool.Activated:Connect(function()

        if Mouse.Target then
		if Mouse.Target:FindFirstChild("Humanoid") then
			
			local who = game.Players:GetPlayerFromCharacter(character)
			print(who)

                    end
		end
	end
    end
end)

2 comments

0

On line 13 it underlines characer and says "Unknown Global 'character' "

0

just create the variable for character on line 03, like "local character = player.Character"

This discussion is preserved as it was. New answers, comments, and votes are closed.