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

How to get a Players name by mouse.Target?

What this script does is when you click on a player in a game it brings up a gui, that gui would be the players profile. Only one problem I don't know how to get the players name from the person they clicked.

I tried this but it did not seem to work. Title.Name = mouse.Target.Parent.Name .. "'s Profile"

The title should appear as "IntellectualBeing's Profile" (Using my name as an example)

wait(1)
local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();
local Frame2 = script.Parent.Content
local Title = Frame2.Title

mouse.Button1Down:connect(function()
    if mouse.Target and mouse.Target.Parent and game.Players:GetPlayerFromCharacter(mouse.Target.Parent) then 
        Frame2.Visible = true; 
   		Title.Name = mouse.Target.Parent.Name .. "'s Profile" --This does not work.
	end
end)

3 answers

✓ Accepted answer

Sometimes your script would not work because first. What if the mouse target was a part inside a hat? We need to make sure we are getting the character. So this one below should work. Also, I don't think numbers inside variable names will not work.

wait()
local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();
local FrameB = script.Parent.Content --I think numbers in variables wont work. Try letters instead.
local Title = FrameB.Title
mouse.Button1Down:connect(function()
	if mouse.Target and mouse.Target.Parent then
		plr=game.Players:GetPlayerFromCharacter(mouse.Target.Parent) 
		if plr~=nil then 
			FrameB.Visible = true
			Title.Text = plr.Name .. "'s Profile" --Works now.
		else
			plr=game.Players:GetPlayerFromCharacter(mouse.Target.Parent.Parent) 
			if plr~=nil then 
				FrameB.Visible = true
				Title.Text = plr.Name .. "'s Profile" --Works now.
			else
				plr=game.Players:GetPlayerFromCharacter(mouse.Target.Parent.Parent.Parent)
				if plr~=nil then 
					FrameB.Visible = true
					Title.Text = plr.Name .. "'s Profile" --Works now.
				end 
			end 
		end
	end
end)

Edit: What if you hit a hat, Thats double in the input.

3 comments

1

Thank's for making my code more efficient! :)

0

Wait, it's not changing the title text?

1

You have it set to change the name of the title. Edited it to change the Title text.

(I can't comment on other answers): @1518NICENICKEY: You can have numbers in the variables as long as the number isn't at the beginning of the variable name.

And, I believe you have a great fix for it. Same idea I had when I saw this.

1 comment

0

Thanks. I know, it sucks when you cant post a comment on other users posts until your reputation is above 10.

it goes like this

wait(1)
local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();
local Frame2 = script.Parent.Content
local Title = Frame2.Title

mouse.Button1Down:connect(function()
    if mouse.Target and mouse.Target.Parent and game.Players:GetPlayerFromCharacter(mouse.Target.Parent) then 
        Frame2.Visible = true; 
        Title.Name = mouse.Target.Parent.Name .. "usernames's Profile" --This does not work.
    end
end)


4 comments

0

I am just trying to get the name of the player that they clicked on.

0

yes in scripting "username" stand for that persons username like "teleport username to chat=sayer.end

0

Well, not really. If I used that for this.. Part.Name = OtherPart.Name .. "username's Profile" It would return "Partusername'sProfile"

1

I'm sorry, daniel. But you have no idea how scripts work. You put "username's" in a string, so it will show "Partusername's Profile". Besides, did you even try to make a script like this? Was it disastrous?

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