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 only make a gamepass work once?

So i've made this gamepass to gives +50000 gold when the person who bought it rejoins/starts. But whenever they rejoin they still get 50000 gold, when they're only sopoused to get 50000 gold once. My Script

local GamePassID = 198893078  --game pass ID
local gps = game:GetService("GamePassService")

function playerJoined(newPlayer)
    if gps:PlayerHasPass(newPlayer,GamePassID) then
		newPlayer.leaderstats.Gold.Value = newPlayer.leaderstats.Gold.Value + 50000
    end
end

game.Players.PlayerAdded:connect(playerJoined)

1 comment

0

Please help!

2 answers

✓ Accepted answer

To do what you're trying to accomplish, we need to set up a DataStore, which we will fill with the users that have bought the GamePass.

First, lets define the DataStore key that we'll be using, and the gamepass required.

local ds = game:GetService("DataStoreService"):GetDataStore("IncreasedGold")
local passId = 198893078

Now, lets have whenever a player joins.. we check if they have the gamepass. And if they have the gamepass, we check if they have data stored already. If they have data stored already it would mean that they've already been awarded the gold because if they don't have data, but have the gamepass.. then we're going to set up data(:

game.Players.PlayerAdded:connect(function(plr)
	wait(1) --Just to be safe
	local key = tostring(plr.userId).."_Bought"
	local data = ds:GetAsync(key)
	local stats = plr.leaderstats.Gold
	if data == nil then
		if game:GetService("GamePassService"):PlayerHasPass(plr,passId) then
			ds:SetAsync(key,true) --The actual value doesn't matter
			stats.Value = stats.Value + 50000
		end
	end
end)

So, lets put this whole script together. It should look like this:

local ds = game:GetService("DataStoreService"):GetDataStore("IncreasedGold")
local passId = 198893078

game.Players.PlayerAdded:connect(function(plr)
	wait(1) --Just to be safe
	local key = tostring(plr.userId).."_Bought"
	local data = ds:GetAsync(key)
	local stats = plr.leaderstats.Gold
	if data == nil then
		if game:GetService("GamePassService"):PlayerHasPass(plr,passId) then
			ds:SetAsync(key,true) --The actual value doesn't matter
			stats.Value = stats.Value + 50000
		end
	end
end)

I also suggest maybe adding a checking with a CharacterAdded Event, so that the player doesn't have to rejoin upon buyig the gamepass.

If you have any other questions feel free to comment on my answer or PM me on the site(:

2 comments

1

Sorry, I didnt know you had to explain most people dont do that with me and I downvoted because I thought you did it cause you thought your script was better so you downvoted mine sorry.

0

It's ok. Just don't do things like that in the future please. And I wouldn't downvote you because I thought my script was better than yours, that's a matter of opinion and abuse of my reputation.

Well what you need to use it a Developer Product. You can use this script and just add the amount of cash you want it to add and also the ID of the Developer product. Dont forget you have to put this script in StarterPack.

local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
 
CASHID = 19382769 -- Put the Developer Product ID here

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_product_" .. receiptInfo.ProductId
	local numberBought = ds:IncrementAsync(playerProductKey, 1)
	for i,v in pairs (game.Players:GetChildren()) do
		if v.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == CASHID then
			
			
				lds = v:FindFirstChild("leaderstats")
				if lds ~= nil then
					cs = lds:FindFirstChild("Money") --Change to your leaderstat
					if cs ~= nil then
						cs.Value = cs.Value + 10 --Change to the amount you want this to add
					end
				end
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

Also here is the script to put in the GUI so that they can buy it:

local MarketplaceService = Game:GetService("MarketplaceService")
local productId = 22137544 -- Dev Product ID
 
script.Parent.MouseButton1Click:connect(function()
	MarketplaceService:PromptProductPurchase(script.Parent.Parent.Parent.Parent, productId)
end)

Make sure that is a local script in the GUI. You can create a Dev Product by going to your game and hitting Configure this place then going to developer products and creating one. If this help please thumbs it up or accept answer!

2 comments

0

Just supplying the asker with a script and saying edit here and here doesn't help them learn. Explain what you're doing.

0

That's sort of childish, don't you think? To downvote me out of spite.

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