Marketplace with DataStore

Hello everyone, I have a problem with creating an Marketplace where people can sell the skins from my game. And, I don’t know how to make it properly so everyone can see list of all sells.
By the way, for the main stats I’ve used Profile Service!

So, firstly, I tried used OrderedDataStore… BUT, I was having a lot of problem.
I’ve made it like this:

local function setSellData(player: Player, username: string?, skinKey: array<>?, gamepassId: number<>?)
	local data = {
		username = username or "", 
		skin = skinKey and skinKey.key or "",
		guid = skinKey and skinKey.id or "",
		gamepass = gamepassId or 0,
		time = os.time()
	}
	local success, result = pcall(function()
		local strId = tostring(player.UserId)
		local dataString, score = HttpService:JSONEncode(data), data.time
		MarketplaceStore:SetAsync(strId, score)
		PlayerDataStore:SetAsync(strId, dataString)
	end)
	if success then
		print("Data stored successfully for player: " .. player.Name)
		print("Stored Data: ", data)
	else
		warn("Failed to store data for player: " .. player.Name)
	end
end
local function updateMarketplace(player: Player)
	local success, pages = pcall(function()
		return MarketplaceStore:GetSortedAsync(false, 100)
	end)
	if success then
		local data = {}
		local top = pages:GetCurrentPage()
		for _, v in ipairs(top) do
			local playerId, score = v.key, v.value
			local success, dataString = pcall(function()
				return PlayerDataStore:GetAsync(playerId)
			end)
			if success and dataString then
				local dataTable = HttpService:JSONDecode(dataString)
				print("Retrieved Data for player: ", dataTable)
				table.insert(data, dataTable)
				
				MarketplaceStore:SetAsync(playerId, dataTable)
			else
				print("Failed to retrieve data for player: " .. playerId)
			end
		end
		print(data)
		Signals.Functions.Marketplace["Set"]:InvokeClient(player, data)
		return data
	else
		print("Failed to get sorted data: " .. tostring(pages))
	end
end
while task.wait(60) do
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.UserId > 0 then
			local success, dataString = pcall(function()
				return MarketplaceStore:GetAsync(tostring(player.UserId))
			end)
			if success and dataString then
				print("Updating Data for player: " .. player.Name)
				if dataString then
					local dataTable = HttpService:JSONDecode(dataString)
					pcall(function()
						MarketplaceStore:UpdateAsync(tostring(player.UserId), function(oldVal)
							return HttpService:JSONEncode(dataTable)
						end)
					end)
				end
			else
				print("Failed to retrieve data for player: " .. player.Name)
			end
		end
	end   
end

So, I was having problem like, ValueNotAllowed, you can’t see listing, etc.
I’ve already tried encoding and decoding, and it still doesn’t work.

A LOT of problem. And, I don’t know if I should try make it with GlobalDataStore…
But if someone can help me, it would be really appreciated!

3 Likes

I think the main problem is, OrderedDataStore cannot read Tables

at most Player’s own table
like

global.userid = {value = number} etc.

but not tables inside tables
like this
image

I am also trying to find a way to READ TABLES so i can reach it

I have no idea how bloxbiz and other Datastore stuff reads Tables, i need to find a way too…

1 Like

Had to change the way and what data it was storing. Hope I didn’t go too far.
Also the way a few things were returning …

Re-Write
local HttpService = game:GetService("HttpService")
local MarketplaceStore = game:GetService("DataStoreService"):GetDataStore("MarketplaceStore")
local PlayerDataStore = game:GetService("DataStoreService"):GetDataStore("PlayerDataStore")

local function setSellData(player, username, skinKey, gamepassId)
	local data = {
		username = username or "", 
		skin = skinKey and skinKey.key or "",
		guid = skinKey and skinKey.id or "",
		gamepass = gamepassId or 0,
		time = os.time()
	}
	local success, result = pcall(function()
		local strId = tostring(player.UserId)
		local dataString = HttpService:JSONEncode(data)
		MarketplaceStore:SetAsync(strId, data.time)
		PlayerDataStore:SetAsync(strId, dataString)
	end)
	if success then
		print("Data stored successfully for player: " .. player.Name)
		print("Stored Data: ", data)
	else
		warn("Failed to store data for player: " .. player.Name)
	end
end

local function updateMarketplace(player)
	local success, pages = pcall(function()
		return MarketplaceStore:GetSortedAsync(false, 100)
	end)
	if success then
		local data = {}
		local top = pages:GetCurrentPage()
		for _, v in ipairs(top) do
			local playerId, score = v.key, v.value
			local success, dataString = pcall(function()
				return PlayerDataStore:GetAsync(playerId)
			end)
			if success and dataString then
				local dataTable = HttpService:JSONDecode(dataString)
				print("Retrieved Data for player: ", dataTable)
				table.insert(data, dataTable)
				MarketplaceStore:SetAsync(playerId, dataTable.time)
			else
				print("Failed to retrieve data for player: " .. playerId)
			end
		end
		print(data)
		Signals.Functions.Marketplace["Set"]:InvokeClient(player, data)
		return data
	else
		print("Failed to get sorted data: " .. tostring(pages))
	end
end

while task.wait(60) do
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.UserId > 0 then
			local success, dataString = pcall(function()
				return MarketplaceStore:GetAsync(tostring(player.UserId))
			end)
			if success and dataString then
				print("Updating Data for player: " .. player.Name)
				local dataTable = HttpService:JSONDecode(dataString)
				pcall(function()
					MarketplaceStore:UpdateAsync(tostring(player.UserId), function(oldVal)
						return dataTable.time
					end)
				end)
			else
				print("Failed to retrieve data for player: " .. player.Name)
			end
		end
	end   
end
2 Likes

Thanks, but I already bought a server in Google Firebase :slight_smile:
(not server but subscription for it)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.