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 detect who clicked a button?

How can I tell who pressed a button?

For example:

script.Parent.MouseButton1Click:Connect(function()
	print("{player who pressed the button's name} has pressed the button")

	game.Players.{player who pressed the button's name}.leaderstats.Cash.Value = 100
end)

2 answers

✓ Accepted answer
  1. You're probably using a ClickDetector so use MouseClick But if you're using a Gui use remoteevents.
  2. As seen in the page, MouseClick always returns playerWhoClicked as an instance.
  3. If it is a guibutton then just send a RemoteEvent to server and then the player value is automatically sent to server. (Be aware of hackers spamming it and getting infinite cash)
script.Parent.MouseClick:Connect(function(plr) -- plr
    print(plr.Name.." has pressed the button")
    plr.leaderstats.Cash.Value = 100
end)

It's quite simple actually! You don't need to add "game.Players" In fact all yo need to do is add a variable in the function parameters.

Here is the code:

script.Parent.MouseButton1Click:Connect(function(plr) print(plr.. " has pressed the button")

  plr.leaderstats.Cash.Value = 100

end)

2 comments

0

Sorry, I forgot to Lua button :)

0

Wrong MouseButton1Click is an element of a GuiButton and won't return player, as it's only clickable for the LocalPlayer also seen on the Page for it it doesn't return the player instance.https://developer.roblox.com/en-us/api-reference/event/GuiButton/MouseButton1Click

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