Possible to save many things under one datastore?

Im currently making a simulator type game and i need to use datastores to save many aspects of it. Such as, currency, the players inventory, and, for example, which pets they currently have equipped. I just wasnt sure the best way to go about saving multiple things like this or if there should be no problems at all?

I ask this because, although im not new to coding in general, im exceptionally bad with datastores.

Thanks for all helps and tips in advance

I like to use a tables and save it that way

local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") -- Create or access the datastore local playerDataStore = DataStoreService:GetDataStore("PlayerData") -- Default data template local function getDefaultData()	return {	Coins = 0,	Level = 1,	Guns = {"Pistol"}, -- Starting gun	Outfits = {"Default"} -- Starting outfit	} end -- Save player data local function saveData(player, data)	local success, err = pcall(function()	playerDataStore:SetAsync(player.UserId, data)	end)	if not success then	warn("Failed to save data for", player.Name, err)	end end -- Load player data local function loadData(player)	local data	local success, err = pcall(function()	data = playerDataStore:GetAsync(player.UserId)	end)	if success and data then	return data	else	if err then warn("Failed to load data for", player.Name, err) end	return getDefaultData()	end end -- When player joins Players.PlayerAdded:Connect(function(player)	local data = loadData(player)	player:SetAttribute("Coins", data.Coins)	player:SetAttribute("Level", data.Level)	player:SetAttribute("Guns", table.concat(data.Guns, ","))	player:SetAttribute("Outfits", table.concat(data.Outfits, ","))	-- Save on leave	player.AncestryChanged:Connect(function(_, parent)	if not parent then	local newData = {	Coins = player:GetAttribute("Coins"),	Level = player:GetAttribute("Level"),	Guns = string.split(player:GetAttribute("Guns") or "", ","),	Outfits = string.split(player:GetAttribute("Outfits") or "", ","),	}	saveData(player, newData)	end	end) end) -- Optional: Save all when game closes game:BindToClose(function()	for _, player in ipairs(Players:GetPlayers()) do	local newData = {	Coins = player:GetAttribute("Coins"),	Level = player:GetAttribute("Level"),	Guns = string.split(player:GetAttribute("Guns") or "", ","),	Outfits = string.split(player:GetAttribute("Outfits") or "", ","),	}	saveData(player, newData)	end end) 

This script is kinda like the one I use. Although I would recommend doing in a module script. To sal things like update data, save data, exc…
(Also wanted to say that for this one, I stored the values in the players attributes which isent entirely nessesarry but it is one way of simplifying things and making it easy :blush:)

1 Like

Thank you, this is very helpful. Ive usually not used tables but it seems like something im going to try, so this is a great example for me to learn from :slightly_smiling_face:

1 Like