How to get player that clicked button?

Hey developers!

I made this script for a donation board. Though I want to know how to get the player that pressed on this button. Is there any parameter for this? My script:

--Variables--
local buttonlist = script.Parent
local marketplaceSV = game:GetService("MarketplaceService")

--Script--
buttonlist:WaitForChild("5RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153796205)
end)

buttonlist:WaitForChild("10RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153796328)
end)

buttonlist:WaitForChild("25RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153796501)
end)

buttonlist:WaitForChild("50RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153796703)
end)

buttonlist:WaitForChild("100RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153797121)
end)

buttonlist:WaitForChild("250RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153797239)
end)

buttonlist:WaitForChild("500RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153797321)
end)

buttonlist:WaitForChild("1000RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(?, 1153797396)
end)

I know normally you would put this in a LocalScript in StarterPlayerScripts, though that doesn’t always fire for me??? Any help would be appreciated!

Have a nice day!

This is a LocalScript, so you know the local player is Players.LocalPlayer.

It’s a serversided script:

It should be a LocalScript then.

Don’t use server scripts for UI

Again, please read what I said:

Why not just put the local script inside the UI? I’m not really understanding here…

MouseButton1Clicked:Connect(function(player)

end)
2 Likes

GuiButton.MouseButton1Click doesn’t pass any arguments, you’re thinking of ClickDetector.MouseClick

@sjr04 @lluckvy

This is my LocalScript. It seems like it doesn’t always fire (already tested with a print):

--Variables--
local buttonlist = game:GetService("Workspace"):WaitForChild("Lobby"):WaitForChild("DonationBoardPart"):WaitForChild("DonationBoardGui")
local localplayer = game:GetService("Players").LocalPlayer
local marketplaceSV = game:GetService("MarketplaceService")

--Script--
buttonlist:WaitForChild("5RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153796205)
end)

buttonlist:WaitForChild("10RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153796328)
end)

buttonlist:WaitForChild("25RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153796501)
end)

buttonlist:WaitForChild("50RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153796703)
end)

buttonlist:WaitForChild("100RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153797121)
end)

buttonlist:WaitForChild("250RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153797239)
end)

buttonlist:WaitForChild("500RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153797321)
end)

buttonlist:WaitForChild("1000RobuxButton").MouseButton1Click:Connect(function()
    marketplaceSV:PromptProductPurchase(localplayer, 1153797396)
end)

It’s a SurfaceGui. LocalScripts don’t run in the Workspace.

Works on my machine:

You can put SurfaceGuis in the StarterGui, set the Adornee of it to the part you want it on, and the localscript will still work

Okay that makes sense, you never specified whether it’s a ScreenGui or SurfaceGui

I didn’t parent the SurfaceGui to StarterGui, but setting the Adornee did fix it?? Kind of unclear to me, but it worked so :+1: :+1: :+1: :+1:!

1 Like

Interesting, I didn’t know that

You should still parent it to StarterGui, so that the LocalScripts when the Gui is cloned into PlayerGui will run and you can use LocalPlayer

It passes the parameters X and Y (the Vector2 position of where the player clicked).

No, I’m fine like this. They already run, they’re in StarterPlayerScripts.

1 Like

Oop- they do not work anymore. Didn’t change anything though?? Also tried parenting it to StarterGui, still didn’t work.