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

How to make a button, that you can click only once after you purchased the game pass?

So I have a script, where player can buy in-game money for robux.

local mps = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
local id = 0000000 -- game pass id
local me = 0000000 -- I can't buy money

script.Parent.MouseButton1Click:Connect(function()
	if not plr.UserId == me then -- if not me
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserID, id) then
			print("Player owns gamepass.")
			local points = plr:FindFirstChild("leaderstats")
			local money = points:FindFirstChild("Money")
			money.Value = money.Value + 1
		else
			mps:PromptPurchase(plr, id) 	
		end
	end
end)

So if player purchased the game pass, script will add one more dollar to the money. But I want to do the script, where after player received one dollar he can't get more money by clicking the button, even if he owns game pass. Even if player will rejoin the game, he can't get more money by clicking the button.

2 answers

I suggest using DataStoreService. Create a Script inside ServerScriptService and the code should be looking like this:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("GamePassOwners")

local function create(plr)
	local userId = plr.UserId
	local key = "Player_" .. userId
	
	local value = Instance.new("BoolValue", plr)
	value.Name = "HasGamePassOn"
	
	local ls = Instance.new("Folder", plr)
	ls.Name = "leaderstats"
	
	local money = Instance.new("IntValue", ls)
	money.Name = "Money"
	
	local data
	local success, result = pcall(function()
		data = DS:GetAsync(userId)
	end)
	
	if success and data then
		value.Value = data[1] or false
		money.Value = data[2] or 0
	else
		print("ERROR!" .. result)
	end
end

local function save(plr)
	local userId = plr.UserId
	local key = "Player_" .. userId
	
	local tableToSave = {
		plr.HasGamePassOn.Value,
		plr.leaderstats.Money.Value
	}
	
	local success, result = pcall(function()
		DS:SetAsync(userId, tableToSave)
	end)
	if success then
		print("Saved!")
	else
		print("Error")
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(create)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then return end
	
	for _, plr in pairs(game.Players:GetPlayers) do
		save(plr)
	end
end)

Then in the gamepass script, it should be like this:

--Must be inside the button and in a LocalScript
local mps = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
local id = 0000000

script.Parent.MouseButton1Click:Connect(function()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserID, id) then
		if plr.HasGamePassOn.Value == false then
			print("Player owns gamepass.")
			local points = plr:FindFirstChild("leaderstats")
			local money = points:FindFirstChild("Money")
			money.Value = money.Value + 1
			plr.HasGamePassOn.Value = true
		else
			print("You already clicked.")
		end
	else
		mps:PromptPurchase(plr, id)
	end
end)

If there are errors or mistakes, please let me know! ^^

I don't quite get what you mean but I cooked something up that might work!

Also by the way, You were applying Money to the client, That won't update to the server so if you try and use that cash, it won't work cause it's client sided!

Add a RemoteEvent to replicatedStorage

Local Script:

local PlayersService = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketplaceService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local player = PlayersService.LocalPlayer

local addMoneyRE = replicatedStorage:FindFirstChild("AddMoney")

local button = script.Parent

local gamePassID = 1 -- Change to gamepass id
local ownerId = 15324 -- Change to your UserId

local fired = false

button.MouseButton1Click:Connect(function()
	if player.UserId ~= ownerId then
		if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) and fired == false then

			addMoneyRE:FireServer(1) -- Change to amount you wanna give

			print(player.Name.." owns item!")

			fired = true

		else
			MarketPlaceService:PromptGamePassPurchase(player, gamePassID)

		end	
	else
		addMoneyRE:FireServer(1) -- Change to amount you wanna give
		
	end
end)

Server Script:

local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage:FindFirstChild("AddMoney").OnServerEvent:Connect(function(player, amount)
	
	player.leaderstats.Cash.Value += amount
	
end)

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