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
)