Surface gui help

So I am making a donate button on a wall.

At first I tried using a local script but nothing happend , then I used a normal script and tried getting the player from the function like this:

FiveButton.MouseButton1Click:Connect(function(player)
	MarketPlaceService:PromptProductPurchase(game.Players[player.Name], FiveId)
end)

But that gave me this error:
20:34:36.837 - Workspace.Part.SurfaceGui.Script:12: attempt to index nil with ‘Name’

Thanks you for reading.

You can try to add an if statement that checks if the player exists then prompt the purchase.
Also when prompting a purchase you should always call it from a local script, because it will show the prompt on the player’s screen.

In surface guis are you suppose to use server scripts?

i don’t think so, they are like ScreenGUIs except on objects.

When I use a local script to test a print statement, it doesn’t print.
And When I tried printing the parameter it comes out as nil.

From what I perceived, you are using a textbutton on a part and using the MouseButton1Click event for your script. This may not work because the event cannot fetch which player is clicking, so the script thinks its nil and gives you that error. A better option is to use a ClickDetector on that part because you can use the MouseClick event and get the player since it can give you the player.

2 Likes

It doesn’t make sense to index the player in the Players if you already have the player instance just saying.

What do you mean by that? (30chars)

This is a common mistake I see when trying to make SurfaceGUI’s, and it’s a simple solution.

First, move your SurfaceGui into StarterGui:
image

Then set it’s Adornee property to the part you’d like it to display on.
image

Now we can script it like you would normally script a TextButton. Add a RemoteEvent to ReplicatedStorage called “PromptPurchase”, and link to it through the TextButton script.

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.PromptPurchase:FireServer()
end)

Like so.

Next, let’s make a connection on the server, make a script in ServerScriptService and link it up to the RemoteEvent.

game.ReplicatedStorage.PromptPurchase.OnServerEvent:Connect(function(player)
	game:GetService("MarketplaceService"):PromptProductPurchase(player, FiveId)
end)
1 Like

you know you can prompt the purchase straight from the client, and specify what happens when the player buys the product in the server.

No need to use an event

Whether possible or not it’s always been a habit of mine to send purchase data to the server as I use an excessive custom analytics module that collects all of the data, besides I like having all of my purchase handling in one script, that being both prompting and processing purchases.

Much cleaner and more organized that way.

Here is what I think is the solution:
You can start by doing what @S_maritan says.
Then we will make it so that when the button is clicked, it will prompt the purchase

 script.Parent.MouseButton1Click:Connect(function()
     game:GetService("MarketplaceService"):PromptPurchase(game.Players.LocalPlayer, productId)
 end)

Then create a script, so we can hook this prompt with the server.

game:GetService("MarketplaceService").ProcessReceipt = function(reciept)  --Here we are hooking the client code with the server
      for _, player in pairs(game.Players:GetPlayers()) do  --Then we are looping through all the players in-game
          if player.UserId == reciept.PlayerId and receipt.ProductId == productId then  --If the player id is the same id of the player that purchased the product then we continue
              --Add code to execute when player purchases product
          end  
      end
end

This is how I do it, hopefully this helps :slight_smile:

i guess each person has his/her way of doing this