PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

PurchaseScript

PurchaseScript

Oct 01 2018, 1:41 AM PST

local CurrencyManager = require(389753980)
local MarketPlaceService = game:GetService("MarketplaceService")
 
local BuyableAssets = require(game.ReplicatedStorage.BuyableAssets)
local buyAssetEvent = game.ReplicatedStorage.CurrencyEvents.BuyAsset
 
local goldDeveloperProducts = {}
goldDeveloperProducts[32909239] = 100
 
local otherDeveloperProducts = {}
 
local wearableAssetLimit = {}
wearableAssetLimit["Hat"] = 3
wearableAssetLimit["Shirt"] = 1
wearableAssetLimit["Pants"] = 1
 
local updateCurrencyDisplayEvent = game.ReplicatedStorage.CurrencyEvents.UpdateCurrencyDisplay
 
local currentGold = {}
 
MarketPlaceService.ProcessReceipt = function(receiptInfo)
	local productId = receiptInfo.ProductId	
 
	if goldDeveloperProducts[productId] then
		local success, purchased = CurrencyManager:PurchaseHandled(receiptInfo.PlayerId, receiptInfo.PurchaseId)
 
		if success and not purchased then
			local success, balance = CurrencyManager:HandleRobuxPurchase(goldDeveloperProducts[productId], receiptInfo)
			if success then
				local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
				if player then
					currentGold[receiptInfo.PlayerId] = balance
					updateCurrencyDisplayEvent:FireClient(player, balance)
				end
				return Enum.ProductPurchaseDecision.PurchaseGranted
			end
		end		
 
	end
 
	if otherDeveloperProducts[productId] then
		-- Handle other dev product purchases
	end
 
	return Enum.ProductPurchaseDecision.NotProcessedYet
end
 
local function onPlayerJoin(player)
	CurrencyManager:InitializePlayer(player.UserId)
	CurrencyManager:CheckDailyLogin(player.UserId)
	local balance = CurrencyManager:GetBalance(player.UserId)
	currentGold[player.UserId] = balance
	updateCurrencyDisplayEvent:FireClient(player, balance)
end
 
local function cleanCharacter(character, assetType)
	local limit = wearableAssetLimit[assetType]
	local objectsFound = 0
	for _, object in ipairs(character:GetChildren()) do
		if object:IsA(assetType) then
			objectsFound = objectsFound + 1
			if objectsFound >= limit then
				object:Destroy()
				objectsFound = objectsFound - 1
			end
		end
	end
end
 
local function onBuyAsset(player, assetId)
	local purchased = CurrencyManager:HasAsset(player.UserId, assetId)
	if not purchased then
 
		if currentGold[player.UserId] >= BuyableAssets.Shirts[assetId].Cost then
			local success = CurrencyManager:GiveAsset(player, assetId)
			if success then
				local success, balance, enoughFunds = CurrencyManager:Withdraw(player.UserId, BuyableAssets.Shirts[assetId].Cost)
				if success then
					purchased = true
					currentGold[player.UserId] = balance
					updateCurrencyDisplayEvent:FireClient(player, balance)
				end
			end
		end
	end	
 
	local character = player.Character
	if character and purchased then
		local asset = game.InsertService:LoadAsset(assetId):GetChildren()[1]
		cleanCharacter(character, asset.ClassName)
		asset.Parent = character
	end
end
 
buyAssetEvent.OnServerEvent:Connect(onBuyAsset)
 
game.Players.PlayerAdded:Connect(onPlayerJoin)
for _, player in ipairs(game.Players:GetPlayers()) do
	onPlayerJoin(player)
end
Tags:
  • currency
  • scripting
  • advanced