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

How do I find a player who clicked a ScreenGui button?

I've made a script, but it isn't working..

script.Parent.MouseButton1Down:connect(function(player)
	local owner = "Player"
	player = playerWhoClicked.Name 
end)

5 answers

✓ Accepted answer

Since I haven't actually seen a proper answer here, I'll do it.

Your problem is that you're trying to get a value of a variable that Does not exist.``playerWhoClicked` (If not defined in unshown code) is not a player. You can't get the name of a normal variable. If you are using a localscript (I hope you are) You should use the following code.

script.Parent.MouseButton1Down:connect(function()
    local owner = "Player"
    player = game.Players.LocalPlayer.Name
end)

Now if this code doesn't work (If you aren't using a LocalScript) You should do the following (Which may or may not work based on your parenting system)

script.Parent.MouseButton1Down:connect(function()
    local owner = "Player"
    player = script.Parent.Parent.Parent.Parent.Name
end)

Hope this helped! If so, accept!

~Chem

your best bet depending what your using a local script so if you did this

script.Parent.MouseButton1Down:connect(function()
local player = game.Players.LocalPlayer
local name= player.Name
-- if your looking for the physical form in workspace you can do this
local player2 = game.Workspace:FindFirstChild(name)
-- and then what ever you wanted to do with it

end)

Note: Remember LocalPlayer can only be used in LocalScripts

2 comments

0

Instead of searching after the players name you can use Player.Character and it will directly connect to the players character in workspace.

0

Yes you could so thanks I could use that next time

First of all, every GUI uses local scripts which mean you can use game.Players.LocalPlayer. Then you just can do player = game.Players.LocalPlayer.

local player = game.Players.LocalPlayer

script.Parent.Button1Down:connect(function()
	local owner = player.Name 
end)

1 comment

0

Not entirely true.

You want to get the player who clicked a GUI Button. To do this, we use game.Players.LocalPlayer. This must be used in a LocalScript. We use the following code:

local plr = game.Players.LocalPlayer

function onbuttonclick()
	print(plr.Name + " clicked this GUI object!"
end
script.Parent.MouseButton1Click:Connect(onbuttonclick)

Put this code inside a LocalScript in your TextButton or ImageButton.

Hope I helped!

-funnytevinn

First off, I've had this same problem. I fixed it by realizing you can't test some GUI functions inside of the Studio. To fix it use this code:

script.Parent.MouseButton1Down:connect(function(player)
local owner = "YOURUSERNAMEGOESHERE"
player = playerWhoClicked.Name
end)

Once you put your user instead of Player save it, then try to play it live. Hope this helped!

3 comments

0

Worst Answer 2k16

0

srry dude, it's bad when the owner says "worst answer".

0

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