Saving folder of Bool Value into datastore

  1. What do you want to achieve? I am creating a find the __ (like find the markers), and I want to create a system where it saves whether or not you have collected a certain marker (im not making find the markers but ill just use it). However, I want to make a system that looks through a folder in the workspace and creates boolvalues with the name of the certain marker; then when you leave or join it saves/loads the values back in.

  2. What is the issue? I already did most of what I am trying to achieve, but I am having trouble with the saving and loading in part. First off, for my saving, whenever I try and print it, all the values return as nil, and I am unsure why. For the loading, I honestly do not know how to load in the values equal to what they were when you left.

  3. What solutions have you tried so far? I looked through the devforum and devhub of tables, metatables, dictionaries, and other people who had this question, but I still could not figure out how to get this to work with the code I have.

my code:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	--Create the bool values 
	for _, child in ipairs(game.Workspace.MainZone.SpiderHorses:GetChildren()) do
		local bool = Instance.new("BoolValue")
		bool.Name = child.Name.." Horse"
		bool.Parent = player:WaitForChild("leaderstats")

	end
	
	local playerUserId = "Player_"..player.UserId
	
	
	-- Load Data
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	
	if success then 
		
			--i have no idea how to load in the data from my previous table
			
		end
		--
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {}
	
	
	-- Saves values that currently exist.

	for _, child in ipairs(player.leaderstats:GetDescendants()) do -- change this to your item
		
		table.insert(data, child.Value)
		
		end
		
		
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then 
		print("Data Successful")
	
	else
		print("Nope")
		warn(errormessage)
	end
	
end)

I think this may work:

local value
game.Players.PlayerAdded:Connect(function(Player)
	local plr = Player
	local ds = game:GetService("DataStoreService"):GetDataStore(plr.Name.."Stats")
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "MarkersCollected" -- You can change the name of 'MarkersCollected' but remember to edit the rest of the code
	leaderstats.Parent = Player
	for _, child in ipairs(workspace.Markers:GetChildren()) do -- Item's location in workspace
		value = Instance.new("BoolValue")
		value.Name = child.Name
		value.Value = false
		value.Parent = leaderstats
	end
	local child=plr.MarkersCollected:GetChildren()
	for i=1, #child do
		child[i].Value = ds:GetAsync(plr.userId..child[i].Name)
	end
end)
game.Players.PlayerRemoving:connect(function(plr)
	local ds = game:GetService("DataStoreService"):GetDataStore(plr.Name.."Stats")
	local child = plr.MarkersCollected:GetChildren()
	for i=1, #child do
		child[i].Value = ds:SetAsync(plr.userId..child[i].Name,child[i].Value)
	end
end)

Just remember to edit the ‘Markers’ with your own stuff, same with location at line 8

1 Like