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

How do I detect who clicked a button?

Asked by
CodeWon 181
3 years ago

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

Log in to vote
1
Answered by
sayer80 457 Moderation Voter
3 years ago
  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)
Ad
Log in to vote
0
Answered by 3 years ago

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)

0
Sorry, I forgot to Lua button :) wolftamer894 50 — 3y
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 sayer80 457 — 3y

Answer this question