Folder in Backpack is nil?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local CardStore = DataStoreService:GetDataStore("PlayerCardList")

local CardService = require(game.ReplicatedStorage.Modules.CardService)

local CardCache = {}

local function createDefaultDeck()

	return {
		{
			["Name"] = "boyparis",
			["Color"] = 6865280869,
			["Display"] = 6873565000,
			["Power"] = 400,
			["Health"] = 5000,
			["WhiteCost"] = 10,
			["RedCost"] = 3,
			["BlueCost"] = 3,
			["GreenCost"] = 3,
			["YellowCost"] = 3,
			["Trigger"] = nil,
			["Effect"] = nil,
			["Model"] = 6877137987
		},

		{
			["Name"] = "nil",
			["Color"] = 6865283271,
			["Display"] = 6865303020,
			["Power"] = 50,
			["Health"] = 50,
			["WhiteCost"] = 1,
			["RedCost"] = 1,
			["BlueCost"] = 1,
			["GreenCost"] = 0,
			["YellowCost"] = 0,
			["Trigger"] = nil,
			["Effect"] = nil,
			["Model"] = 6877137987
		}
	}

end

local function playerAdded(player)
	local success, data = pcall(function ()
		return CardStore:GetAsync(player.UserId)
	end)
	if success then
		if data then
			CardCache[player.UserId] = data
		else
			CardCache[player.UserId] = createDefaultDeck()
		end
	else
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end

	

	for _, card in pairs(CardCache[player.UserId]) do
		local card = CardService.Unserialize(card)
		card.Position = UDim2.new(0,0,0,0)
		card.Parent = player.Backpack.Deck
print(player)
	end
	
	CardService.CardCache = CardCache

end

Players.PlayerAdded:Connect(playerAdded)


I have a problem with the playeradded function in this script, it says Deck is not a valid member of Backpack despite being there and using :WaitForChild(“Deck”) makes the script yield forever
image
image

It is likely that Deck is not replicated in time and the infinite yield issue kicks in after a certain amount of time that is really short. Try adding a second argument to fix that.

However, I’m rather confused why you are storing that in Backpack and not anywhere else.

I was not sure where else to place it :v
Ok i’ll try that as soon as i can

If your folder is in starter pack, it will work just fine.

Also,

That means the you need to use :WaitForChild("Folder"). Basically your trying to get something that hasn’t been replicated to the client yet.

It shouldn’t make it yield forever. But iirc if it yields for more than a certain amount of time, then it will give you a warning in the output.

And as Operatik has said,

Why do you need a folder in backpack? Theres a probably a better way to do what you are trying to do.

1 Like

have you tried repeat wait() until?

1 Like

Weird, the output does not show up, but the rest of the function doesn’t run either.
Also, the folder is storing gui elements

It’s bad practice to do this. Instead, use events to wait for something to happen. Or in this case, use :WaitForChild().

More info on this here:

Alternatively, try storing every possible GUI elements in a single folder in ReplicatedStorage and then allow server to store the cards properties if they ever are fixed. If that’s not the case and the cards have unique values, that’s a little more complicated.

Once you have the “compressed” information, you can just replicate the cards to the player to assumingly hold them visible from the client whenever the client requests them. The request will ask the server to check its cache of cards for the player and then replicate as accordingly and correspondingly. They will access the folder with the GUI elements, which then will be cloned to the player’s “hand”.

Yeah that’s what im doing, the cards are customized so the script generates them from the cache, and i thought doing that as soon as possible would be the better option, I wanted to do this to remove the gui elements from the folder as the cards were drawn, but i think i could just transfer the cache to a module that handles player interactions that removes the cards from said list and resets upon a match ending.

Remember to secure that the cache on client is not exploited and allow the server to control for what card is being played. :slight_smile:

1 Like