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
4 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:

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

Log in to vote
1
Answered by 4 years ago
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)
0
ty imaA6D 39 — 4y
Ad
Log in to vote
0
Answered by 4 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 — 4y
0
But theres nothing to print the name... iiConstable_Subwayx 130 — 4y
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 — 4y
Log in to vote
0
Answered by 4 years ago
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.

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

Answer this question