game.ReplicatedStorage.RE.marketPlace.OnServerEvent:Connect(function(D,n)spawn(function()local X,b=getAsset(n)if not X then print("\078\111\116 \112\114\111\100\117\099\116 \047\047"..n)return end local J=Datastore:GetData(D)local r=MarketplaceService:UserOwnsGamePassAsync(D.UserId,X.Id)if J and table.find(J.Gamepasses or{},X.Id)then return onProductPurchased(D,X.Id)end if r then return onProductPurchased(D,X.Id)end game.ReplicatedStorage.RE.GamepassNotification:FireClient(D)if b=="\080\097\115\115\101\115"then MarketplaceService:PromptGamePassPurchase(D,X.Id)elseif b=="\080\114\111\100\117\099\116\115"then MarketplaceService:PromptProductPurchase(D,X.Id)end end)end)game.Players.PlayerAdded:Connect(function(D)local n while not Datastore do Datastore=shared.Datastore wait()end while not n do if not D.Parent then return end n=Datastore:GetData(D)wait(1)end local X=n.Gamepasses or{}for b,J in pairs(script.Passes:GetChildren())do local r=getAsset(J.Name)local G=r.Id local e=table.find(X,G)if e or MarketplaceService:UserOwnsGamePassAsync(D.UserId,G)then if not e then table.insert(n.Gamepasses,G)end if r.onJoined then r.onJoined(D)end game.ReplicatedStorage.RE.gamepassUpdate:FireClient(D,getNameFromID(G))end end end)
-- init
game.ReplicatedStorage.RE.marketPlace.OnServerEvent:Connect(function(player, name)
	spawn(function()
		local product, class = getAsset(name)
		if not product then
			print("Not product //" .. name)
			return
		end

		local data = Datastore:GetData(player)

		local t = MarketplaceService:UserOwnsGamePassAsync(player.UserId, product.Id)

		if data and table.find(data.Gamepasses or {}, product.Id) then
			return onProductPurchased(player, product.Id) -- already owned
		end

		if t then
			return onProductPurchased(player, product.Id)
		end
		game.ReplicatedStorage.RE.GamepassNotification:FireClient(player)
		if class == "Passes" then
			MarketplaceService:PromptGamePassPurchase(player, product.Id)
		elseif class == "Products" then
			MarketplaceService:PromptProductPurchase(player, product.Id)
		end
	end)
end)

game.Players.PlayerAdded:Connect(function(player)
	local data

	while not Datastore do
		Datastore = shared.Datastore
		wait()
	end

	while not data do
		if not player.Parent then
			return
		end

		data = Datastore:GetData(player)
		wait(1)
	end

	local gamepasses = data.Gamepasses or {}
	for i, module in pairs(script.Passes:GetChildren()) do
		local pass = getAsset(module.Name)
		local id = pass.Id

		local found = table.find(gamepasses, id)
		if found or MarketplaceService:UserOwnsGamePassAsync(player.UserId, id) then
			if not found then
				table.insert(data.Gamepasses, id)
			end
			if pass.onJoined then
				pass.onJoined(player)
			end

			game.ReplicatedStorage.RE.gamepassUpdate:FireClient(player, getNameFromID(id))
		end
	end
end)
