Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a button to buy a gamepass and change a leaderstat?

I was trying to make a gui button to buy a game pass and when the game pass is bought it adds +1 to your stage. I couldn't get it to work, when you click it nothing happens. Is there something wrong with it? Can somebody help me? Here is the code:

local ITEM_ID = 244697986
local STAGE = script.Parent.Parent.Parent.Parent.leaderstats.Stage
local MarketplaceService = game:GetService("MarketplaceService")
local player = script.Parent.Parent.Parent.Parent

function onButtonClicked()
	if STAGE.Value == 47 then return end
MarketplaceService:PromptPurchase(ITEM_ID)
wait(.5)
if MarketplaceService:PlayerOwnAsset(player, ITEM_ID) then
STAGE.Value = STAGE.Value +1
wait(.1)
player.Character.Humanoid.Health = 0
wait(5.2)
script.Parent.Parent:remove()
end
end

script.Parent.MouseButton1Click:connect(onButtonClicked)

Please help me fix it! :(

1 answer

✓ Accepted answer

I changed a few things like added a listener for PromptPurchaseFinished" and instead of killing the "Humanoid" I just used the "BreakJoints" Method on the "Character".

local ITEM_ID = 244697986
local STAGE = script.Parent.Parent.Parent.Parent.leaderstats.STAGE
local MarketplaceService = game:GetService("MarketplaceService")
local player = script.Parent.Parent.Parent.Parent

MarketplaceService.PromptPurchaseFinished:connect(function(Player, assetId ,isPurchased) -- Listen for the sale...
	if isPurchased and Player == player and assetId == ITEM_ID then --if the player did pay...
		STAGE.Value = STAGE.Value + 1
		if player.Character then
			player.Character:BreakJoints() -- BreakJoints instead.
		end
	end
end)

function onButtonClicked()
    if STAGE.Value == 47 then return end
	MarketplaceService:PromptPurchase(ITEM_ID)
end

script.Parent.MouseButton1Click:connect(onButtonClicked)

I hope this helped!

2 comments

0

Thx :D

0

Np : )

This discussion is preserved as it was. New answers, comments, and votes are closed.