Buy button in surfacegui isn't working (Solved)

So basically I want to do when you click on a buy button in like specific surfacegui and all it prompts the game pass, but the script I made isn’t working.

local GamepassDisplay = game.Workspace.GamepassDisplay.GamepassDisplay1
local SurfaceGui = GamepassDisplay:FindFirstChild(“SurfaceGui”)
local Frame = SurfaceGui:FindFirstChild(“Frame”
local BuyButton = Frame:FindFirstChild(“Buy”)
local function onButtonClicked()
game:GetService(“MarketplaceService”):PromptGamePassPurchase(game.Players.LocalPlayer, 157205217)
end
BuyButton.MouseButton1Click:Connect(onButtonClicked)
image

Your problem is placing the LocalScript under Workspace.

1 Like

What do you mean by that?

eyrtuegrtfeydt0eryryuestuy

Move it to somewhere listed on the documentation; LocalScripts do not run under Workspace, which is why nothing is happening

But the reason this should works is because the Workspace object is a special object that represents the virtual environment where the game is running. When you use game.Workspace to access an object in the Workspace, you’re actually getting a reference to that object in the virtual environment, not in the Workspace hierarchy in the Explorer panel.

So even though the LocalScript is located in the GamepassDisplay1 part and the GamepassDisplay model is located in the Workspace, the LocalScript is still able to access the GamepassDisplay model using game.Workspace.GamepassDisplay.

Please post this on Scripting Support.

how

uywares8ygt8rtgagyi8artwegiywergy8er97qeh

Go to this topic, edit it, and move it to Scripting Support.

Being able to access an object from your script doesn’t equate to the script being able to run in the same location.

Your solution is to just move it somewhere else the documentation lists

To say it again,

To clarify, a LocalScript cannot be run in the Workspace because it’s a client-side script and the Workspace is part of the server-side simulation. However, in the case of the code we wrote earlier, the LocalScript is placed in a part that’s a child of the GamepassDisplay model, which is in the Workspace.

When the game is running, the LocalScript is executed on the client and is able to access the GamepassDisplay model through the game.Workspace object. However, the LocalScript is not actually running in the Workspace - it’s running on the client, while the GamepassDisplay model is on the server.

LocalScript doesn’t work inside Workspace. Instead, move the SurfaceGUI to StarterGUI and set the Adornee property to the part you want to display. Move the LocalScript inside SurfaceGUI.

ChatGPT (assuming) is not a reliable source of information!

1 Like

If I was using that I wouldn’t be here.

Where do I put the localscript then?

As I said, inside the SurfaceGUI.

I’ve never seen this phenomenon before. You’re the OP, but you’re telling somebody, who is trying to help you, how to script? That’s not how it works, 99.9% of the time. What we’re trying to tell you is that LocalScripts do not run in the Workspace.

1 Like

As of now, I changed the script

local GamepassDisplay = game.Workspace.GamepassDisplay.GamepassDisplay1
local SurfaceGui = game.StarterGui:FindFirstChild(“SurfaceGui1”)
local Frame = SurfaceGui:FindFirstChild(“Frame”)
local BuyButton = Frame:FindFirstChild(“Buy”)
local function onButtonClicked()
game:GetService(“MarketplaceService”):PromptGamePassPurchase(game.Players.LocalPlayer, 157205217)
end
BuyButton.MouseButton1Click:Connect(onButtonClicked)

and doesn’t work

Read this, @YT_PlasaYara.

1 Like

Don’t use StarterGUI like this in the script.

local GamepassDisplay = game.Workspace.GamepassDisplay.GamepassDisplay1
local SurfaceGui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("whatever")
local Frame = SurfaceGui:FindFirstChild(“Frame”)
local BuyButton = Frame:FindFirstChild(“Buy”)
local function onButtonClicked()
game:GetService(“MarketplaceService”):PromptGamePassPurchase(game.Players.LocalPlayer, 157205217)
end
BuyButton.MouseButton1Click:Connect(onButtonClicked)

If you are new to programming, basically any ScreenGUI inside StarterGUI replicates to the player joined. It replicates inside PlayerGui which is present inside the player.

It just works like this

game.Players.PlayerAdded:Connect(function(player)
local ScreenGUI = game.ServerStorage.ScreenGUI --example
ScreenGUI:Clone().Parent = player.PlayerGui
end)
1 Like

Thanks, it works.

rdgdfhfgjhftjyfd