Need help with datastore

So I have all playerdata saved under the PlayerDataFolder which is in the serverstorage and which goes PlayerDataFolder → Players data folder → Inventories data.

local replicatedstorage = game:GetService("ReplicatedStorage")
local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("DATASTORE")
local serverstorage = game:GetService("ServerStorage")
local AllPlayerData = serverstorage:WaitForChild("AllPlayerData")
local defaultstat = 0 


game.Players.PlayerAdded:Connect(function(player) --Player stats will add more later + Autosave loop 
	
	local PlayerDataFolder = Instance.new("Folder")
	PlayerDataFolder.Parent = AllPlayerData
	PlayerDataFolder.Name = player.Name

	local leaderboard = Instance.new("Folder")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = player
	
	local PlayerInventory = Instance.new("Folder")
	PlayerInventory.Name = "Inventories"
	PlayerInventory.Parent = PlayerDataFolder
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderboard

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderboard

	local death = Instance.new("IntValue")
	death.Name = "Death"
	death.Parent = leaderboard
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderboard
	
	local tries = 0
	local data
	local success, errormessage 
	
	repeat success, errormessage = pcall(function()
			data = datastore:GetAsync(player.UserId)
			tries += 1
			task.wait(1)
		end)
	until success or tries >= 3
	
	if success == false then 
		player:Kick("Unable to load data successfully")
		warn(errormessage)
		return
	end

	wins.Value = data.wins or defaultstat
	kills.Value = data.kills or defaultstat
	death.Value = data.death or defaultstat
	coins.Value = data.coins or defaultstat
	
	for i, folders in AllPlayerData:GetChildren() do
		local folderclone = folders:Clone()
		folderclone.Parent = player 
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local tries = 0
	local success, errormessage 
	local playerfolder = player.Name
	local data = {wins = player.leaderstats.Wins.Value, kills = player.leaderstats.Kills.Value, death = player.leaderstats.Death.Value, coins = player.leaderstats.Coins.Value} --might not work}
	
	repeat
		success, errormessage = pcall(function()
			datastore:SetAsync(player.UserId, data)
			tries += 1
			task.wait(1)
		end)
	until success or tries >= 3 
	
	if success == false then 
		warn(errormessage)
	end
end)
  • What potential improvements have you considered?
    I have considered making the data variable more flexible so it can store more things other than leaderboard values.
  • How (specifically) do you want to improve the code?
    I want more fail saves and logic that prevents data loss and make process of data saving more consistent and can anyone explain if I need a new key for saving players inventory.

Hey, first of all you should use Bind:Toclose(). If you don’t know what BindTclose() is,it is a function binding another function prior to the game shutting down. Usually you would make a loop a :Kick() all the players in the game to trigger the game.Players.PlayerRemoving event or directly save data. You can make your own DataStore obviously but if you’re planning to save a good amounts of data on a larger scale using library such as ProfileService is something i would recommend. These libraries are thoroughly tested to prevent data loss and are optimized.

1 Like

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