Is there a better way to check for gamepass purchases in the current session?

So basically, I started a script that adds a dictionary to a table called “robuxItemPurchasesThisGame” or something like that, and then when a gamepass perk is eligible, such as a cash collection in this case, it checks to see if the player has bought the gamepass based on the table, as well as the normal MarketplaceService PlayerOwnsGamePass request (I haven’t added either of these two checks yet), and I was wondering, does Roblox still need you to rejoin the game to realize if a player bought something in that game session?

Here's what I'm doing currently
--Defining Variables
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local playerInfoDataStore = DataStoreService:GetDataStore("PlayerInfo")

local robuxItemsBoughtThisGameSession = {}

game.Players.PlayerAdded:Connect(function(player) -- Gives player cash when joining game
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	local success, err = pcall(function()
		local playerData = playerInfoDataStore:GetAsync(player.UserId)
		if playerData then
			coins.Value = playerData
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player) -- Saves cash when leaving game
	local success, err = pcall(function()
		playerInfoDataStore:SetAsync(player.UserId,player.leaderstats.Coins.Value)
	end)
end)

local function addToPurchasedItemsTable(player,id) -- Function for gamepasses and devproducts being added to the list of purchased products
	if table.find(robuxItemsBoughtThisGameSession,player.UserId) then
		table.insert(robuxItemsBoughtThisGameSession[player.UserId],#robuxItemsBoughtThisGameSession[player.UserId]+1,id)
	else
		table.insert(robuxItemsBoughtThisGameSession,#robuxItemsBoughtThisGameSession+1,{player.UserId})
		print(robuxItemsBoughtThisGameSession)
		table.insert(robuxItemsBoughtThisGameSession[player.UserId],1,id)
	end
end

local function returnPurchasedState(player,id)
	print("Returning")
	local remoteEvent = game.ReplicatedStorage:WaitForChild("PurchaseEvent")
	remoteEvent:FireClient(player,id)
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player,id,wasPurchased) -- When gamepass is purchased, run function
	local playerName = game.Players:GetNameFromUserIdAsync(player)
	if wasPurchased then
		addToPurchasedItemsTable(game.Players[playerName],id)
		returnPurchasedState(player,id)
	end
end)

MarketplaceService.PromptProductPurchaseFinished:Connect(function(player,id,wasPurchased)-- When a dev product is purchased, run function
	print(wasPurchased)
	print(player)
	local playerName = game.Players:GetNameFromUserIdAsync(player)
	if wasPurchased then
		addToPurchasedItemsTable(game.Players[playerName],id)
		returnPurchasedState(player,id)
	end
	
	if id == 1177590313 then
		player.leaderstats.Coins.Value += 500
	end
end)



local purchaseEvent = game.ReplicatedStorage:WaitForChild("PurchaseEvent") -- Purchases from the client request
purchaseEvent.OnServerEvent:Connect(function(player,purchaseType,id)
	if purchaseType == "DevProduct" then
		MarketplaceService:PromptProductPurchase(player,id)
	elseif purchaseType == "Gamepass" then
		MarketplaceService:PromptGamePassPurchase(player,id)
	end
end)
1 Like