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

How to do I make it so when I click a players Torso/Humanoid, it prints out their username?

What I am trying to do here is when you click a players Torso/Humanoid, it will print out there username. But I can't find or even create the right function for it. Any help? (Also I probably messed up on some of the functions so if I did then please fix it thank you.)

code:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local kind = "Name"
local char = game.Workspace:FindFirstChild(plr.Name)

mouse.Button1Down:connect(function()
	if mouse.Target ~= nil then
		if kind == "Name" then
			local g = mouse.Target.Parent
			if g:FindFirstChild("Humanoid") then
				-- Where I am stuck on
			end
		end
	end
end)

3 answers

✓ Accepted answer
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local kind = "Name"
local char = game.Workspace:FindFirstChild(plr.Name)

mouse.Button1Down:connect(function()
    if mouse.Target ~= nil then
        if kind == "Name" then
            local g = mouse.Target.Parent
            if g:FindFirstChild("Humanoid") then
                print(g.Name) --prints the name of the parent, aka the player
            end
        end
    end
end)

1 comment

0

ty

So in all honesty your code works fine. If you were testing it on yourself in studio then that would be where the error occurred for you as it wont work on the player who is clicking.

Side note: In order to test things like this when you are developing by yourself there is a feature in roblox studio under test which is called clients and servers and it allows you to test as multiple players as once.

3 comments

0

ty

0

But theres nothing to print the name...

0

True but in regards to the functionality of the code everything works its just if they tried testing it with the print() line being in there on themselves it would appear not to function when it does.

local mouse = player:GetMouse{}
local plr = game.Players:GetPlayers()

mouse.Button1Down:Connect(function()
	for i, v in pairs(plr) do --loops through the players in a server and checks if it is equal to the target parent
		if mouse.Target.Parent == v then
			print(v)
		else 
			--ignores result
		end
	end
end)

I believe this works, but I have heard that mouse.Target doesn't work on players, but try this.

2 comments

0

ty

0

Shawn it does just the player can't click on them self

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