What is the best way to store Game data on roblox using datastore

Here you can find some infromation about DataStores: Data Stores | Documentation - Roblox Creator Hub

1 Like

there isn’t really a “best way” for a datastore, it matters of what you are trying to save and how complex it is

1 Like

Reading this article has defiantly broadened my understanding of datastores, But Id still like some help with like tips on how to make it easier in the long run. Do you maybe have some sample code on large datastores and how you handle them?

Okay i get ill have to come up with my own way of doing it cause every games different in terms of storage. But like I’m just looking for tips to not have any excess data. Or like the best ways to search through large datastores. Just Stuff like that

local Player = game:GetService("Player")
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
        leaderstats.Name = "leaderstats"
        
       local coins = Instance.new("IntValue", player)
       coins.Name = "Coins"

      local success, error = pcall(function()
             DataStore:GetAsync("Player_"..player.userId)
      end)

      if(success) then
            print("Saved Data")
      else
            print("Something went wrong")
            warn(error)
      end
end)

Players.PlayerRemoving:Connect(function(player)
	local success, error = pcall(function()
              DataStore:SetAsync("Player_"..player.userId, player.leaderstats.Coins.Value)
       end)
       if(success) then
            print("Saved Data")
      else
            print("Something went wrong")
            warn(error)
      end
end)

This is just an example on how I would do it, but if you want more values stored, you should add Tables.

That’s another thing I’m trying to work out, Is it best to Store Values as Values in folders in like player folders and have scripts read/write data from there using Changed events.

Or just keep data strictly in scripts.

idk which ones more efficient/easier but i know having it in folders can maybe have problems with game security

Well, it depends what you want to do. I would want to have the values stored in a folder in player, for me its just easier. But it really depends on you what you find better.

Okay so im obviously not going to go through with everything I do but i wanna show you an idea of How i would store my data and give you an idea of how much data I’m planning on storing. (Please dont go into reading it its just to show Structure and Size).


DefultStorage = {
	
	ShelterSave = 1,
	
	ShelterInfomation = {
		TimePlayed = 0,
		ShelterName = "Unnamed",
		Premium = false
	},
	
	Inhabitants = {
		
		[1] = {
			
			Basics = {
				Name = "Beth", 
				Level = 1, 
				Health = 100, 
				Gender = "F",
				AssignedRoom = "Outside"
			},				
			Misc = {
				AcceptedInside = false, 
				Dead = false, 
				Exploring = false
			},			
			Inventory = {
				Weapon = "Fists", 
				Clothes = "ShelterSuit", 
				Watch = "None"
			},		
			STIFF = {
				Strength = 1, 
				Toughness = 1,
				Inteligence = 2,
				Fitness = 2,
				Fortune = 2
			},
			
		},
		
		[2] = {

			Basics = {
				Name = "Jake", 
				Level = 1, 
				Health = 100, 
				Gender = "M",
				AssignedRoom = "Outside"
			},				
			Misc = {
				AcceptedInside = false, 
				Dead = false, 
				Exploring = false
			},			
			Inventory = {
				Weapon = "Fists", 
				Clothes = "ShelterSuit", 
				Watch = "None",
			},		
			STIFF = {
				Strength = 2, 
				Toughness = 2,
				Inteligence = 1,
				Fitness = 1,
				Fortune = 2
			},

		},
		
		[3] = {

			Basics = {
				Name = "Alex", 
				Level = 1, 
				Health = 100, 
				Gender = "M",
				AssignedRoom = "Outside"
			},				
			Misc = {
				AcceptedInside = false, 
				Dead = false, 
				Exploring = false
			},			
			Inventory = {
				Weapon = "Fists", 
				Clothes = "ShelterSuit", 
				Watch = "None" 
			},		
			STIFF = {
				Strength = 2, 
				Toughness = 2,
				Inteligence = 1,
				Fitness = 1,
				Fortune = 2
			},

		}
		
	},
	
	Inventory = {
		Food = 100,
		Water = 100,
		Power = 100,
		Weapons = {},
		Outfits = {},
		Watches = {},
		Corks = {},
		SuitCases = 0,
		WatchBox = 0				
	},
	
	ShelterRooms = {
		[1] = {
			RoomType = "VaultDoor",
			Level = 1,
			InHabitantSpace = 2,
			Coordinates = {
				"A:1",
				"A:2",
				"A:3",
				"A:4"
			}
			
		}
	},
	
	Quests = {}
}

So here’s my prototype of the layout, is this a smart way of doing it or is there a better way?

(Also this is quick type, please dont bully my formating :frowning: )

I don’t know any other/better way, instead you want to do it with Values. But thats a way that I would definitely use.

I really want too, its just I’m worried I’m going to have to make a load of Remote Events / Functions And I wanna work out the easiest approach before starting.

Well, you could make everything in 1 script, wich would be a big script, but you would have so many Remote Events/Functions. I think it would be easier to put it in 1 script since you can detect errors faster, and it is just not so much effort.

I always use JSONDecode and JSONEncode for my saves. You can store tables in a single string value by using them.

I did look into it. But According to this thread Can you datastore dictionaries? Apparently ROBLOX does it automatically.

Saved data can be stored as a table (and often is). The difference is that with JSON, the data will be in a single stringvalue instead of a folder with many int/stringvalues

I Don’t really understand enough about it. or the best way to include it. Do you have anywhere that explains it better than the Roblox Wiki. Cause there it just says its good for encrypting and decrypting.

Or maybe some sample code of how you/Someone has used it for converting to a single string value?

pretty simple concept

-- useful function v
_G.additem=function(classname,properties)
	local item=Instance.new(classname)
	for i,v in pairs(properties) do
		item[i]=v
	end
	return item
end

local http=game:GetService("HttpService")
local defaultstats={Level=1,XP=0}
local stringvalue=_G.additem("StringValue",{Value=http:JSONEncode(defaultstats)}) -- string now contains the table

local tabl=http:JSONDecode(stringvalue.Value)
print(tabl.Level) -- 1

I use it for all my games.

2 Likes

Okay thanks this seems like a really good way of doing it

My brain has kind of fallen asleep But can it Do lists inside of lists or is it better to make multiple strings instead?

I frequently do tables inside of tables. It works perfectly fine.

Okay thanks i think i should be fine on my own now. Thanks for all your help

This whole method of persistence using the DataStore seems like a broken way to go about things. I tried posting a feature request for a “GameStorage” Service (similar to the ServerStorage Service), but it wont let me.

1 Like