Data Store, Saving Folders and Values

I’m working on a project where the player is able to create an account using a name and password. When the player makes an account a folder gets added to another folder in Rep storage, the folder has two values username and password so the player can log back in after leaving and joining back. The problem I’m having is saving the folders to a database and loading them back in after the player leaves.

I know you can’t save instances to a database so I would have to save the properties but I don’t know how to save properties or the amount of folders in the rep storage folder.

Any help would be greatly appreciated because I have been stuck on this problem for a little while and haven’t made any progress.

You can make a table, and take the children’s values, and put them in the table.
Ex:

> Folder (Folder)
  > Username (StringValue)
  > Password (StringValue)

Script:

local folder = --Identify it here
local newTable = {}

for i, v in ipairs(folder:GetChildren()) do
    newTable[v.Name] = v.Value
end

You can instead get the children from the rep storage folder and loop through all the children and add the new ones to the table, so the table would have all the children’s name. Like this.

local PlayerList = game.ReplicatedStorage.Players – folder name

local NewTable = {} – making a new table

game.Players.PlayerRemoving:Connect(function() – Getting when the player leaves

for i, v in pairs(PlayerList:GetChildren()) do

if table.find(NewTable, v.Name) then

else

table.insert(DataBaseTable, v.Name)		
		
for i, B in pairs(v:GetChildren()) do
	
DataBaseTable[B.Name] = B.Value	

end

end

end

print(DataBaseTable)

end)

This would work but I’m wanting every child from V to be connected V and its just adding V’s children to the table.

Sorry, but can you reiterate what you are trying to do.

Also, what does connect mean?

Sorry about that when I was typing it, I knew it wasn’t going to make much sense I have that problem figured out using what you showed me. Or sorta what you showed me I had to change it a bit
image_2022-10-30_183424257

This is how I did it, But now I need to figure out how to put these into a datastore

Sorry for the late response, I am really bad at explaining datastores, however there are many resources to help with this, like this video (AlvinBlox) and this post for datastore. Or if you prefer datastore2, this video from AlvinBlox (again) and this post.

Hope this helps!

Neither of the video’s or post explain how you can save an instance such as a folder and the children inside of the folder. I don’t want to waste your time but would you be able to just make the script for me and help me understand it?

We already got the children. no? And assuming we are just using instances with .Value, we have all the values in a table, and I believe saving tables is mentioned in all the links.

And sorry, I cannot write a script right now, however I could get it done by tomorrow if you want.

Yes, I was thinking about saving the instance instead of a table, however both would work.

You cannot save instances.
You can either save single pieces of data, or a table of data.

function SaveData(Player)
	local DataTable = {}
	for i, v in pairs(Player.Folder:GetChildren()) do
		DataTable[v.Name] = {
		["Pass"] = v.Pass.Value, 
		["PriColor"] = v.PriColor.Value, 
		["SecColor"] = v.SecColor.Value, 
		["Bio"] = v.Bio.Value, 
		["Pass"] = v.Pass.Value,,
		["UserId"] = v.UserId.Value -- Why is this getting saved? Is it needed?
		}
	end
	DataStore:SetAsync(Player.UserId, DataTable)
end

function LoadData(Player)
	local Data
	
	local Success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	
	if Success and Data ~= nil then
		
		for i, v in pairs(Data) do
			-- bla bla bla, im just gonna skip to loading the data into whatever youre loading it into
			NewThing.Pass.Value = v.Pass
			NewThing.PriColor.Value = v.PriColor
			NewThing.SecColor.Value = v.SecColor
			NewThing.Bio.Value = v.Bio
			NewThing.Pass.Value = v.Pass
			NewThing.UserId.Value = v.UserId
		end
		
	elseif Success and Data == nil then
		-- Players has no saved data
	else
		--  An error happened while loading data.
	end
end
1 Like

This script didn’t work I changed a couple of things such as the parenting of the new folders and it gave me this error " This experience is ineligible for the immersive ads beta. Please stay tuned for the full release. " I think it would just work saving the folder with the players name and nothing else. I can do the rest I just need an example.

One thing I noticed is this script is saving it to a key and that’s the player id so when the player joins it can look for the player id and load everything in. What I’m wanting is the playerlist folder to not only have everyone’s account from the server but for everyone that has ever made an account.

If thats what you want, you could load the data, then check if the player’s data exists in the returned table, and if so, delete it.

local Data
local Success, errorMessage = pcall(function()
    Data = DataStore:GetAsync('ServerKey')
end)

if Success and Data ~= nil then
    if table.Find(Data, Player.UserId) then
        table.remove(Data, table.Find(Data, Player.UserId))
    end
end

Then, reinsert the new data, and update the datastore.

Data[Player.UserId] = NewPlayerData
DataStore:SetAsync('ServerKey', Data)

The data should look something like this:

Data = {
[123456789] = {['Stuff in table']};
[234567890] = {['Stuff in table']};
}

However, DataStores do have limits for the amount of stored data per key (I think) which is why using UserIds is the recommended option.

Also, I havent ever used DataStores like this, but i would assume data loss would be VERY common using then llike this.
Im just going to give a useful list to hopefully avoid most problems you may encounter using this:

  1. DO NOT save players game data this way (Money, Inventory items, etc)
  2. Attempt to keep the saved data to a minimum. A few numbers or strings per player at most
  3. Only update the datastore when needed, do not call it every few minutes / seconds.
  4. Keep all data-related stuff to be on the server, unless its required that the clients need information.

Save their UserId, as players can change their username, and then they will lose their data, and exploiters could also (maybe) cheat and use other players data for themselves.

Also with this error, it wouldnt be coming from the code i gave you, as it doesnt use the immersive ads?

This is what my code looks like
Capture
and I’m getting this error


and its coming from

“PlayerList[Player.UserId] = {PasswordTxt, UsernameTxt}”

Im just going to rewrite it, because im seeing things that I explained poorly.

local Ds = game:GetService("DataStoreService"):GetDataStore("PlayerInfoData")

-- Create the player list and update it
local PlayerList = {}

function UpdatePlayerList()
    local Success, ErrorMessage = pcall(function()
        PlayerList = Ds:GetAsync("ServerKey")
    end)
    if Success and PlayerList == nil then
        -- Reset the player list in case there is no data, just to avoid errors.
        PlayerList = {}
    end
end
UpdatePlayerList()

-- Call this function when required to save a players information.
function AddToPlayerList(Player, PasswordTxt, UsernameTxt)
    if table.find(PlayerList, Player.UserId) then
        table.remove(PlayerList, table.find(PlayerList, Player.UserId))
    end
    PlayerList[Player.UserId] = {["Password"] = PasswordTxt; ["Username"] = UsernameTxt}
    Ds:SetAsync("ServerKey", PlayerList)
end

-- For an Example, When a remote event gets fired with the password and username, use the function

-- Example server line:
--game.ReplicatedStorage.SaveAccountInfo.OnServerEvent:Connect(AddToPlayerList)
-- Example client line:
--game.ReplicatedStorage.SaveAccountInfo:FireServer("MyPassword", "MyUsername")

while task.wait(60) do
    -- we have this here to make the servers somewhat line up
    -- If you understand MessagingService, i would recommend using that to sync data instead.
    UpdatePlayerList()
end

Essentially, what this should do, if used properly, is save and load player usernames and passwords.
the UpdatePlayerList function is used to update the list of saved passwords and usernames.

And the AddToPlayerList function is used to save, or overwrite a players password and username.

This should be in a script, inside ServerScriptService, and for clients to communicate with the server, use remote events. I have put in some examples of what to do if you do use remote events.

(Please note, i didnt write this in studio, so there may be errors, slight spelling mistakes and whatnot)

Thanks for helping brother, means a lot :kissing_heart:

Hey partner, I have one last question the table is set up with the player id and a table connected to it with the password and stuff how do I match the password when logging in like table.find(PlayerList, PasswordTxt) how do I do that?

To check things such as the passwords, you can easily do this line of code:

PlayerList[Player.UserId].Username
PlayerList[Player.UserId].Password

To check the password, a function like this should work:

function CheckPassword(Player, Username, Password)
    if PlayerList[Player.UserId] and PlayerList[Player.UserId].Username == Username and PlayerList[Player.UserId].Password == Password then
        print('Password and Username correct')
    else
        print('Either Password or Username are incorrect')
    end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.