Prevent dictionary from overwriting?

Question:

How do I prevent this from overwriting?

data[playerID] = {[Gamepass] = true}

function MarketplaceService:GetOwnedPasses(player)
	local playerID = player.UserId
	local data = {}
	data[playerID] = {}
	for Gamepass,ID in pairs(Items.Gamepasses) do
		if Marketplace:UserOwnsGamePassAsync(playerID, ID) then
			data[playerID] = {[Gamepass] = true}
		else
			data[playerID] = {[Gamepass] = false}
		end
	end
	return data
end

What you are doing is replacing their table with a new table. Add to the current one.

for game_pass, id in pairs(Items.Gamepasses) do
    data[playerID][game_pass] = Marketplace:UserOwnsGamePassAsync(playerID, id)
end

I also would recommend proper exception handling via pcall since the :UserOwnsGamePassAsync method makes a web request and may fail.
Also just a tiny nit pick “game pass” is two words not one :stuck_out_tongue:

1 Like

It still overwrites it? (I could have possibly done it incorrectly though)

function MarketplaceService:GetOwnedPasses(player)
	local playerID = player.UserId
	local data = {}
	data[playerID] = {}
	for Gamepass,ID in pairs(Items.Gamepasses) do
		data[playerID][Gamepass] = Marketplace:UserOwnsGamePassAsync(playerID, ID)
	end
	return data
end

function MarketplaceService:GetOwnedPassesOnly(player)
	local gamepassData = self:GetOwnedPasses(player)
	for a,b in pairs(gamepassData) do
		for c,d in pairs(b) do
			return c,d
		end
	end
end

remove the part where you assign an empty table. because that would clear the entire thing.

also make the table global, instead of creating and sending a new one inside the function. that way you are getting all previously checked gamepasses instead of only the one you are currently checking.

1 Like