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

How to get the name of a player who clicked a button?

Asked by 9 years ago

I want to get the name of the player who clicked a button. Once I get their name I can add Coins to their stash. How do I do this? This is the code I have written so far. (I just need the name part) Thanks!

1function onClicked()
2 
3game.Players.[I NEED NAME HERE].leaderstats.Coins.Value = game.Players.[I NEED NAME HERE].leaderstats.Coins.Value + 25
4 
5end
6script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
9 years ago

ClickDetectors have a built-in parameter for which player clicked the button. All you have to do is add it in.

1function onClicked(player)
2 
3game.Players:FindFirstChild(player.Name).leaderstats.Coins.Value = game.Players:FindFirstChild(player.Name).leaderstats.Coins.Value + 25
4 
5end
6script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
Going indirectly through their .Name is silly. Just use `player.leaderstats` BlueTaslem 18071 — 9y
0
I had that, but when I double checked on the wiki it was using so I worried it would be wrong. Pyrondon 2089 — 9y
Ad

Answer this question