Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
imaA6D 39
5 years ago

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:

01local plr = game.Players.LocalPlayer
02local mouse = plr:GetMouse()
03local kind = "Name"
04local char = game.Workspace:FindFirstChild(plr.Name)
05 
06mouse.Button1Down:connect(function()
07    if mouse.Target ~= nil then
08        if kind == "Name" then
09            local g = mouse.Target.Parent
10            if g:FindFirstChild("Humanoid") then
11                -- Where I am stuck on
12            end
13        end
14    end
15end)

3 answers

Log in to vote
1
Answered by 5 years ago
01local plr = game.Players.LocalPlayer
02local mouse = plr:GetMouse()
03local kind = "Name"
04local char = game.Workspace:FindFirstChild(plr.Name)
05 
06mouse.Button1Down:connect(function()
07    if mouse.Target ~= nil then
08        if kind == "Name" then
09            local g = mouse.Target.Parent
10            if g:FindFirstChild("Humanoid") then
11                print(g.Name) --prints the name of the parent, aka the player
12            end
13        end
14    end
15end)
0
ty imaA6D 39 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

0
ty imaA6D 39 — 5y
0
But theres nothing to print the name... iiConstable_Subwayx 130 — 5y
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. deth836231 142 — 5y
Log in to vote
0
Answered by 5 years ago
01local mouse = player:GetMouse{}
02local plr = game.Players:GetPlayers()
03 
04mouse.Button1Down:Connect(function()
05    for i, v in pairs(plr) do --loops through the players in a server and checks if it is equal to the target parent
06        if mouse.Target.Parent == v then
07            print(v)
08        else
09            --ignores result
10        end
11    end
12end)

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

0
ty imaA6D 39 — 5y
0
Shawn it does just the player can't click on them self deth836231 142 — 5y

Answer this question