Help with OnClicked()

Hello there! this is my first post so please don’t be harsh if I did something wrong.
So my problem is that I want to be able to detect who clicked on a GUI button…
I know on proximity prompts there’s something like this:

local part = script.Parent
local ProximityPrompt = part.ProximityPrompt
ProximityPrompt.Triggered:Connect(function(player)
–whatever can go here
end)

so basically what I’m asking is how I could do something like this but with a GUI button thingy.
If you have any questions please feel free to comment.

Here’s an article that should teach you everything about GUI buttons and how to use them:

Alternatively, you could use code similar to this:

local button = script.Parent
local function buttonClicked()
     -- Do whatever you need to do when the button is clicked
end
button.Activated:Connect(buttonClicked)

How can I use the following line when button activated:

local oppo = game.Players.LocalPlayer.leaderstats.Money
oppo.Value = oppo.Value +1

I’m not completely sure if this is what you’re asking, but you could try this code in a LocalScript parented to the button.

local button = script.Parent
local function buttonClicked()
     local oppo = game:GetService("Players").LocalPlayer.leaderstats.Money
     oppo.Value += 1
end
button.Activated:Connect(buttonClicked)

Yup! Lol thank you for the help. :smile:

1 Like

If you do it this way, only you will see your money. (pretty much useless; it’s not really there for everyone to see)

Try this, instead:

1. Place this in ServerScriptService, as a Server Script.
local AddMoney = game.ReplicatedStorage:WaitForChild([[AddMoney]])

AddMoney.OnServerEvent:Connect(function(Player, Stat, Amount)
   Stat.Value += Amount
end)
  1. Place a RemoteEvent named “AddMoney” in ReplicatedStorage
Place this in a Gui Button, as a LocalScript
local AddMoney = game.ReplicatedStorage:WaitForChild([[AddMoney]])

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild([[leaderstats]])
local Money = leaderstats:WaitForChild([[Money]])
local Amount = 1

script.Parent.MouseButton1Down:Connect(function()
   AddMoney:FireServer(Money,Amount)
end)
2 Likes