DataStore For Folders

  1. What do you want to achieve?
    So, I have a codes GUI that adds a BoolValue into a folder inside the player when they redeem the code. I want to save that codes that are inside the folder so they can’t redeem them again but don’t know how to.

  2. What is the issue?
    The way I scripted it is not working. (Don’t know much about DataStores)

  3. What solutions have you tried so far?
    I tried using :GetChildren() but that doesn’t seem to work

Note: I commented the parts that I tried to save codes

local Data = game:GetService("DataStoreService"):GetDataStore("Test1")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Plr)
	local Statistics = Instance.new("Folder")
	Statistics.Name = "leaderstats"
	Statistics.Parent = Plr
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Statistics
	
	local Codes = Instance.new("Folder") -- Codes Folder
	Codes.Name = "Codes"
	Codes.Parent = Plr
	
	local codesToSave = Codes:GetChildren() -- Get Codes In Folder
	
	local uniqueKey = "id-"..Plr.UserId
	local getSaved = Data:GetAsync(uniqueKey)
	if getSaved then
		Cash.Value = getSaved[1]	
	else
		local valuesToSave = {
			Cash.Value,
			codesToSave, -- Codes
		}
		Data:SetAsync(uniqueKey, valuesToSave)
	end
end)

Players.PlayerRemoving:Connect(function(Plr)
	local uniqueKey = "id-"..Plr.UserId
    local valuesToSave = {
		Plr.leaderstats.Cash.Value,
		Plr.Codes:GetChildren(), -- Codes
	}
    Data:SetAsync(uniqueKey, valuesToSave)
end)

You can put a table inside valuesToSave. Think of tables just like a hierarchy.

local valuesToSave = {}
valuesToSave["Cash"] = Cash.Value,
valuesToSave["Codes"] = {}
-- Now set your codes in. Example, codes ABC and 123:
valuesToSave["Codes"]["ABC"] = true  -- Already used code
valuesToSave["Codes"]["123"] = false  -- Has not redeemed code yet

Something like that. And if you have folders inside your folder, you just set that part to a table, just like we did with the "Codes" part.

Hope that helps

1 Like