How to make cache global variable

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

local CardStore = DataStoreService:GetDataStore("PlayerCardList")

local CardService = require(script.CardHandler)

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

	print(CardCache[player.UserId])
local pos = 0
	for _, card in pairs(CardCache[player.UserId]) do
		local card = CardService.Unserialize(card)
		card.Position = UDim2.new(pos,0,0,0)
		pos += 0.18
		print(card)
		-- Never really recommend interacting with PlayerGui from a server script
		-- especially if it's to modify a Gui and not for a special reason.
		card.Parent = player.Backpack.Deck
	end
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
	playerAdded(player)
end



This is my script, what i wanted to know is how cn I make CardCache a global variable and then how could I access it?

Global variables as in _G? I don’t quite understand what you mean.

Hello. You can use roblox’s global shared or lua’s global _G to do so. Make CardCache a table inside shared:

shared.CardCache = {}

Then you can call it whenever you like.

shared.CardCache[Player.UserId] = data
1 Like

_G is usually regarded as bad practice, modules are the way to go with this.

I’m unsure how you’d set data in a module. Is that possible?

It’s what modules are all about! Lets say you had a variable:

module.Lol = false

In a server script, you can set this to true:

require(modulelol).Lol = true

I’ve noticed its a common misconception that you either cant or shouldn’t change variables in a module.

3 Likes

And what about shared? Is it the same? Also, why is _G not recommended?

I never got into shared, so I couldn’t answer that question.

_G can be used, but since a better module option exists, its better to go with modules. _G still works and you definitely can use it, but modules tend to be easier and look nicer.

1 Like

Yeah that seems like the way to go, as im already using a module anyways.