Tips / Help on saving multiple data

Hello everyone!

My game is about collecting little characters, when you collect them you should be able to rejoin and already have them collected. I have this code in ServerScriptService that inserts multiple boolvalues to say if the player already has the character.

local noobNoob = Instance.new("BoolValue",noobs)
noobNoob.Name = "noobNoob"
noobNoob.Value = false

(There are about 11 of these with different names. The noobs variable is a folder located in the player)

What would be an efficient way to save and load this information? Should I save when the player collects the character? Or something else? Im super new to roblox developing and have never messed with datastores.

Thanks,
Alex.

Datastore Tutorial for Beginners - Resources / Community Tutorials - DevForum | Roblox

1 Like

For any kind of data persistence (saving a player’s data between individual visitations to your game), you’ll need to make use of the DataStore service. The following is the official documentation of the DataStore service:

As said, you must use data store to save and load data, here is something very simple

local DataStore = game:GetService("DataStoreService"):GetDataStore("NoobsStorage")

function LoadData(player)
	local Folder = Instance.new("Folder")
	Folder.Name = "noobs"
	
	--// Get data
	local success, Data = pcall(DataStore.GetAsync, DataStore, player.UserId)
	if not success then		warn(Data)		Data = {}		end

	--// Load
	for _, noob in pairs(Data or {}) do
		local BV = Instance.new("BoolValue")
		BV.Name = noob
		BV.Value = true
		BV.Parent = Folder
	end
	Folder.Parent = player
end

function SaveData(player)
	--// Get table
	local ToSave = {}
	for _, V in pairs(player.noobs:GetChildren()) do
		if V:IsA("BoolValue") and V.Value then
			table.insert(ToSave, V.Name)
		end
	end
	
	--// Save data
	local success, err = pcall(DataStore.SetAsync, DataStore, player.UserId, ToSave)
	if not success then		warn(err)		end
end

In the LoadStorage function you can remove the objects from the map if the player has them.
You can also use attributes instead of instances, in the end it is the same in this case.

1 Like

edit: dang i got beat xD
Well, I just started using data stores a couple weeks ago so ima try and help you with my current knowledge of data stores. So heres a basic data store:

-- made by kinglol123468
local DataStoreService = game:GetService("DataStoreService")
local NoobDataStore = DataStoreService:GetDataStore("NoobDataStore") -- the data store the data will be saved in.
local Players = game.Players
Players.PlayerAdded:Connect(function(Player)
	local NoobFolder = Player:WaitForChild("Noobs") -- Change noobs to whatever the folder is named.
	local PlayerId = "Player_" .. Player.UserId -- the key the data will be saved in.
	local PlayerData
	local Success, ErorrMsg = pcall(function()
		for i,v in pairs(NoobFolder:GetChildren()) do
			if v:IsA("BoolValue") then
				PlayerData = NoobDataStore:GetAsync(PlayerId) -- sets the bool value to what it was before.
				v.Value = PlayerData
			end
			
		end
		
	end)
end)
local function SaveData(Player)
	local NoobFolder = Player:WaitForChild("Noobs") -- Change noobs to whatever the folder is named.
	local PlayerId = "Player_" .. Player.UserId -- the key the data will be saved in.
	local Success, ErorrMsg = pcall(function()
		for i,v in pairs(NoobFolder:GetChildren()) do
			if v:IsA("BoolValue") then
				NoobDataStore:SetAsync(PlayerId, v.Value)
			end
		end
	end)
	if Success then
		print("Saving data was sucessful!")
	else
		print(ErorrMsg)
	end
end
Players.PlayerRemoving:Connect(SaveData) -- save the data when the player is leaving.

before:
image
after:
image

I think this is a step in the right direction however I have been having this issue and cant seem to get around it. Similar to other games my game has a “Met the owner” badge type thing. In my game it gives you a noob (the noob’s name is “alexNoob”) and as the owner I get given this as soon as i join.

Everything has been modified a bit to fit my code:

function LoadData(player)
	local noobs = player.noobs:GetChildren()

	--// Get data
	local success, Data = pcall(DataStore.GetAsync, DataStore, player.UserId)
	if not success then		warn(Data)		Data = {}		end

	--// Load
	for _, noob in pairs(Data or {}) do
		print(noob)
		print(Data)
	end
	
	print("Loaded " .. player.Name .. "'s data")
end

function SaveData(player)
	--// Get table
	local ToSave = {}
	for _, V in pairs(player.noobs:GetChildren()) do
		if V:IsA("BoolValue") and V.Value then
			table.insert(ToSave, V.Name)
		end
	end

	--// Save data
	local success, err = pcall(DataStore.SetAsync, DataStore, player.UserId, ToSave)
	if not success then		warn(err)		end
	
	print("Saved " .. player.Name .. "'s data")
end

Players.PlayerAdded:Connect(function(plr)
	local progress = Instance.new("Folder",plr)
	progress.Name = "NoobProgress"
	
	local thawIce= Instance.new("BoolValue",progress)
	thawIce.Value = false
	thawIce.Name = "ThawIce"
	local onFire = Instance.new("BoolValue", progress)
	onFire.Value = false
	onFire.Name = "OnFire"
	
	local noobs = Instance.new("Folder",plr)
	noobs.Name = "noobs"
	
	local noobNoob = Instance.new("BoolValue",noobs)
	noobNoob.Name = "noobNoob"
	noobNoob.Value = false
	local alexNoob = Instance.new("BoolValue",noobs)
	alexNoob.Name = "alexNoob"
	alexNoob.Value = false
	local coolNoob = Instance.new("BoolValue",noobs)
	coolNoob.Name = "coolNoob"
	coolNoob.Value = false
	local dapperNoob = Instance.new("BoolValue",noobs)
	dapperNoob.Name = "dapperNoob"
	dapperNoob.Value = false
	local fireNoob = Instance.new("BoolValue",noobs)
	fireNoob.Name = "fireNoob"
	fireNoob.Value = false
	local ghostNoob = Instance.new("BoolValue",noobs)
	ghostNoob.Name = "ghostNoob"
	ghostNoob.Value = false
	local greenscreenNoob = Instance.new("BoolValue",noobs)
	greenscreenNoob.Name = "greenscreenNoob"
	greenscreenNoob.Value = false
	local groupNoob = Instance.new("BoolValue",noobs)
	groupNoob.Name = "groupNoob"
	groupNoob.Value = false
	local iceNoob = Instance.new("BoolValue",noobs)
	iceNoob.Name = "iceNoob"
	iceNoob.Value = false
	local iceyNoob = Instance.new("BoolValue",noobs)
	iceyNoob.Name = "iceyNoob"
	iceyNoob.Value = false
	local invertedNoob = Instance.new("BoolValue",noobs)
	invertedNoob.Name = "invertedNoob"
	invertedNoob.Value = false
	local rainbowNoob = Instance.new("BoolValue",noobs)
	rainbowNoob.Name = "rainbowNoob"
	rainbowNoob.Value = false
	local treeNoob = Instance.new("BoolValue",noobs)
	treeNoob.Name = "treeNoob"
	treeNoob.Value = false
	
	LoadData(plr)
end)
Players.PlayerRemoving:Connect(function(plr)
	SaveData(plr)
end)

I have no idea where it went wrong. I dont know if its the loading or the saving but when I join console is printed with this message.

  16:45:04.952   â–Ľ  {
                    [1] = "alexNoob"
                 }  -  Server - NoobHandler:17
  16:45:04.952  Loaded alexwarrior317's data  -  Server - NoobHandler:20

I have no idea why the data table it is getting alexNoob and not any other data. Ive check and it also does not update the alexNoob boolvalue (untill the part of the script that handles the “met the owner noob” runs) or any other noob.

Does anyone know why this might be happening?

Thanks,

  • Alex