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

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

Asked by 8 years ago

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

Log in to vote
2
Answered by 8 years ago

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

Ad
Log in to vote
0
Answered by 8 years ago

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

0
Instead of searching after the players name you can use Player.Character and it will directly connect to the players character in workspace. Kryddan 261 — 8y
0
Yes you could so thanks I could use that next time johndeer2233 439 — 8y
Log in to vote
0
Answered by
Kryddan 261 Moderation Voter
8 years ago

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)
0
Not entirely true. ChemicalHex 979 — 8y
Log in to vote
0
Answered by 4 years ago

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

Log in to vote
-3
Answered by 8 years ago

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!

0
Worst Answer 2k16 evaera 8028 — 8y
0
srry dude, it's bad when the owner says "worst answer". rexbit 707 — 8y
0
HE HE HE http://goo.gl/2B3wMq ChemicalHex 979 — 8y

Answer this question