game.ReplicatedStorage.RE.marketPlace.OnServerEvent:Connect(function(Y,b)spawn(function()local e,s=getAsset(b)if not e then print("\078\111\116 \112\114\111\100\117\099\116 \047\047"..b)return end local C=Datastore:GetData(Y)local V=MarketplaceService:UserOwnsGamePassAsync(Y.UserId,e.Id)if C and table.find(C.Gamepasses or{},e.Id)then return onProductPurchased(Y,e.Id)end if V then return onProductPurchased(Y,e.Id)end game.ReplicatedStorage.RE.GamepassNotification:FireClient(Y)if s=="\080\097\115\115\101\115"then MarketplaceService:PromptGamePassPurchase(Y,e.Id)elseif s=="\080\114\111\100\117\099\116\115"then MarketplaceService:PromptProductPurchase(Y,e.Id)end end)end)game.Players.PlayerAdded:Connect(function(Y)local b while not Datastore do Datastore=shared.Datastore wait()end while not b do if not Y.Parent then return end b=Datastore:GetData(Y)wait(1)end local e=b.Gamepasses or{}for s,C in pairs(script.Passes:GetChildren())do local V=getAsset(C.Name)local j=V.Id local h=table.find(e,j)if h or MarketplaceService:UserOwnsGamePassAsync(Y.UserId,j)then if not h then table.insert(b.Gamepasses,j)end if V.onJoined then V.onJoined(Y)end game.ReplicatedStorage.RE.gamepassUpdate:FireClient(Y,getNameFromID(j))end end end)

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)
